1

我想在我的 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();
4

2 回答 2

1

尝试使用Select类。

Select select = new Select(driver.findElement(By.id("accountList"));
select.selectByVisibleText("text-goes-here");
//or
select.selectByIndex(number);
//or
select.selectByValue("value");
于 2013-09-05T13:09:20.680 回答
1

尝试先点击selectbutton标签

然后你可以找到元素

于 2018-07-16T14:42:06.170 回答