【问题标题】:Can't find an element in a dropdown list - Selenium在下拉列表中找不到元素 - Selenium
【发布时间】:2018-07-16 22:42:06
【问题描述】:

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

【问题讨论】:

    标签: html xpath selenium firebug


    【解决方案1】:

    尝试使用Select 类。

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

    【讨论】:

    • 我尝试使用 select 类,但在这种情况下它不起作用。我尝试了 value 和 index 都没有任何运气......
    • 怎么不行?您看到了什么错误和/或异常?
    • 我得到的错误是找不到源。我在网站的其他地方有相同类型的列表,但也找不到。
    • 您确定 Select 类已正确导入吗?您应该有 org.openqa.selenium.support.ui.Select 代表 Java 或类似的其他语言显示在您的导入中。
    【解决方案2】:

    尝试先点击selectbutton标签

    然后你就可以找到元素了

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多