Two similar dynamic drop down list selection selenium web driver 2.0

5,361 views
Skip to first unread message

Srav

unread,
Dec 27, 2012, 12:10:50 PM12/27/12
to seleniu...@googlegroups.com
I have two dynamic drop down list which are similar, I'm unable to select from the second drop down list. Below is the html:
<div id="list" class="x-list">
<div id="list-list" class="x-list-list-ct">
<ul>
<li class="x-item" role="option">US Dollar - USD</li> 
<li class="x-item x-list-selected x-list-item-over" role="option">Afghan Afghani - AFN</li> //<--this is my 1st selection from 1st dropdown list
<li class="x-item" role="option">Albanian Lek - ALL</li>
<li class="x-item" role="option">Algerian Dinar - DZD</li>
<li class="x-item" role="option">Angolan Kwanza - AOA</li>
<li class="x-item" role="option">Argentine Peso - ARS</li>
</ul>
</div>
</div>

<div id="list" class="x-list">
<div id="list-list" class="x-list-list-ct">
<ul>
<li class="x-item x-list-selected x-list-item-over" role="option">US Dollar - USD</li> //<--this is my default hover selection
<li class="x-item" role="option">Afghan Afghani - AFN</li> 
<li class="x-item" role="option">Albanian Lek - ALL</li>
<li class="x-item" role="option">Algerian Dinar - DZD</li>
<li class="x-item" role="option">Angolan Kwanza - AOA</li>
<li class="x-item" role="option">Argentine Peso - ARS</li>
</ul>
</div>
</div>

When a element is selected or hovered the class name changes to 'x-item x-list-selected x-list-item-over'

For the first list I use driver.findElement(By.xpath("//li[text()[contains(.,'Afghan Afghani - AFN')]]")).click();

When I do the same for second list, the element gets selected but the list doesn't close.

SantoshSarma

unread,
Dec 27, 2012, 12:38:12 PM12/27/12
to seleniu...@googlegroups.com
Try like this

For selecting from first list

   
 driver.findElement(By.xpath("//div[1]/div/ul/li[text()='Afghan Afghani - AFN']")).click();


For selecting from second list

   
 driver.findElement(By.xpath("//div[2]/div/ul/li[text()='Afghan Afghani - AFN']")).click();

Oscar Rieken

unread,
Dec 27, 2012, 2:48:52 PM12/27/12
to seleniu...@googlegroups.com
I would submit a defect to your devs W3C standards note id's should be unique

id = name [CS]
This attribute assigns a name to an element. This name must be unique in a document.

http://www.w3.org/TR/html401/struct/global.html#h-7.5.2


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/KnnxAsOZZDYJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Message has been deleted
Message has been deleted

Srav

unread,
Dec 28, 2012, 12:36:41 PM12/28/12
to seleniu...@googlegroups.com
Bhushan I tried closing by clicking on a static element, but it isn't helping me.
Santosh : Creating the dynamic xpath on div elements isn't helping me, because the number of div elements are dynamic.

kranthi kondle

unread,
Dec 28, 2012, 12:42:12 PM12/28/12
to seleniu...@googlegroups.com
even dynamic xpath we can handle.
Can u send ur application screenshot and html code?

On Fri, Dec 28, 2012 at 11:06 PM, Srav <srav...@gmail.com> wrote:
Bhushan I tried closing by clicking on a static element, but it isn't helping me.
Santosh : Creating the dynamic xpath on div elements isn't helping me, because the number of div elements are dynamic.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/dXbuVtCkP38J.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Regards
Kranthi Kondle

Srav

unread,
Dec 28, 2012, 1:14:16 PM12/28/12
to seleniu...@googlegroups.com
I have four dropdown menus on this page.
Because the second and third are similar, I'm having a hard time selecting the 4th drop down menu item.
Screenshot_1.png
Screenshot_2.png

Srav

unread,
Dec 28, 2012, 2:15:04 PM12/28/12
to seleniu...@googlegroups.com
@Kranthi 

For my second drop down list I use the below 
driver.findElement(
By.xpath("//label[text()[contains(.,'Currency')]]/../../table/tbody/tr/td[1]/input"))
.click();
WebElement we = driver.findElement(By
.xpath("//li[text()[contains(.,'US Dollar - USD')]]"));
we.click();

For my third drop down list I use 
driver.findElement(
By.xpath("//label[text()[contains(.,'Currency')]]/../../table/tbody/tr/td[2]/input"))
.click();
WebElement we = driver.findElement(By
.xpath("//li[text()[contains(.,'US Dollar - USD')]]"));
we.click();


For the above code I get an error saying element cannot be found.

So I alternatively started using this, which is working for now
driver.findElement(By
.xpath("//label[text()[contains(.,'Currency')]]/../../table/tbody/tr/td[2]/div"));

Keyboard kb = ((RemoteWebDriver) driver).getKeyboard();
kb.pressKey(Keys.RETURN);

So for now this is selecting the first default in the list, that is US Dollar-USD, But if I want to select another currency I am using kb.pressKey(Keys.ARROW_DOWN); So per say if I want to select a 10th in list I need to use kb.pressKey(Keys.ARROW_DOWN); 10 times, which definitely doesn't make sense.

Let me know if you have any questions.

Andy Doan

unread,
Jan 2, 2013, 7:52:24 PM1/2/13
to seleniu...@googlegroups.com
I agree with Oscar, ids should be unique, but here is a workaround that I have used with many elements look the same.

Try using driver.findElements(locator), and then WebElement.findElements() or WebElement.findElement()

Something like ...

List<WebElement> allULs = driver.findElements(By.tagname("ul"));
WebElement secondUL = allULs.get(1);
WebElement thirdUL = allULs.get(2);
List<WebElement> allLIsFromSecondUL = secondUL.findElements(By.tagname("li"));
//search through LIs, click on matching LI
for (WebElement singleLI: allLIsFromSecondUL ) {
            if (singleLI.getText().equals("Afghan Afghani - AFN"))
                    singleLI.click();

Manoj Hans

unread,
Jan 3, 2013, 5:21:20 AM1/3/13
to seleniu...@googlegroups.com
+1 for andy...


--Manoj Hans

Srav

unread,
Jan 3, 2013, 10:51:32 AM1/3/13
to seleniu...@googlegroups.com
Thanks Andy, will give it a try and let you know.
Reply all
Reply to author
Forward
0 new messages