Veronica
Veronica

Reputation: 11

Can't find an element in a dropdown list - Selenium

I want to find an element in a dropdown list in my Selenium test. I use firebug to find the xpath, but my Selenium code can not find the xpath. I am looking for option value number 2 in the html code below:

<div id="accountList_gbselect" class="gb-select text-luc3" style=""> 
    <a class="text-luc3" href="#">Konto: xxxxxx    0 kr</a>
    <select id="accountList" style="width: 300px; display: none;" onchange="submit();" size="1" name="accountList">
        <option value="1xxxxx" selected="selected">Konto: 1xxxxxx    0 kr</option>
        <option value="1xxxxx">Testname: 1xxxxx    6 771 kr</option>
        <option value="5xxxxx">Konto: 5xxxxx    500 kr</option>
    <div class="list">
</div>

In my Selenium code I use findElement and I have tried xpath and cssSelector. But can not get it to work. This is how I thought I should write to find the element in the dropdown list:

driver.findElement(By.xpath(".//*[@id='accountList']/option[2]")).click();

Upvotes: 1

Views: 3764

Answers (2)

Oceane
Oceane

Reputation: 11

try to click the select or the button tag first

and then you can find the element

Upvotes: 1

Mark Rowlands
Mark Rowlands

Reputation: 5453

Try using the Select class.

Select select = new Select(driver.findElement(By.id("accountList"));
select.selectByVisibleText("text-goes-here");
//or
select.selectByIndex(number);
//or
select.selectByValue("value");

Upvotes: 1

Related Questions