无法在下拉列表中找到元素 - Selenium

时间:2013-09-05 12:41:38

标签: html xpath selenium firebug

我想在Selenium测试中的下拉列表中找到一个元素。我用firebug找到xpath,但是我的Selenium代码找不到xpath。我在下面的html代码中寻找选项值2:

<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>

在我的Selenium代码中,我使用了findElement,我尝试过xpath和cssSelector。但无法让它发挥作用。这就是我认为我应该写下来在下拉列表中找到元素的方式:

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

2 个答案:

答案 0 :(得分:1)

尝试使用Select类。

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

答案 1 :(得分:1)

尝试首先单击selectbutton标记

然后您可以找到元素

相关问题