java 从 <li> 下拉列表中选择值 - Selenium

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34791599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:21:22  来源:igfitidea点击:

Selecting value from <li> dropdown - Selenium

javaseleniumdrop-down-menuselenium-webdriver

提问by NewCoder888

I'm trying to select a value from a small dropdown that appears to be a "ul" Here's what I see in Firebug:

我正在尝试从一个看起来像“ul”的小下拉列表中选择一个值,这是我在 Firebug 中看到的:

    <button class="btn btn-default dropdown-toggle" aria-expanded="true"       aria-haspopup="true" data-toggle="dropdown" type="button">
<ul class="dropdown-menu">
<li>
<a href="javascript:void(0)">1</a>
</li>
<li>
<a href="javascript:void(0)">2</a>
</li>
<li>
<a href="javascript:void(0)">3</a>

Here's what I have so far, in Java:

到目前为止,这是我在 Java 中所做的:

 public void selectPhoneType(String option)
    {
        driver.findElement(By.className("caret")).click();
        new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.className("dropdown-menu")));
        WebElement element = driver.findElement()
    }

Let's say I wanted to select/click on option "1". How would I complete my code? I haven't worked with "li" too much so I tried using a select, which obviously didnt work. Any help would be appreciated! Thanks

假设我想选择/单击选项 "1"。我将如何完成我的代码?我没有过多地使用“li”,所以我尝试使用选择,这显然不起作用。任何帮助,将不胜感激!谢谢

回答by misterjake

Try something like this:

尝试这样的事情:

public void selectPhoneType(String option) {
       // Open the dropdown so the options are visible
        driver.findElement(By.className("dropdown-menu")).click();
        // Get all of the options
        List<WebElement> options = driver.findElements(By.xpath("//ul[@class='dropdown-menu']/li"));
        // Loop through the options and select the one that matches
        for (WebElement opt : options) {
            if (opt.getText().equals(option)) {
                opt.click();
                return;
            }
        }
        throw new NoSuchElementException("Can't find " + option + " in dropdown");
}

Note: Selenium's Select() won't work here because your list is not under a 'select' tag.

注意:Selenium 的 Select() 在这里不起作用,因为您的列表不在“选择”标签下。

回答by Роман Гуйван

Based on how it's selected in the application you either should simulate mouse click with element.click()or simulate keydown/keyup event with element.sendKeys(Keys.ENTER);Also be aware that touch events may handle differently by your application (click may not work) Here's a similar question

根据它在应用程序中的选择方式,您应该使用模拟鼠标单击element.click()或模拟 keydown/keyup 事件element.sendKeys(Keys.ENTER);另外请注意,您的应用程序可能会以不同方式处理触摸事件(单击可能不起作用) 这是一个类似的问题

回答by Leon Barkan

driver.FindElement(By.XPath("//ul[@class='dropdown-menu']/li/a")).Click();

回答by Leon Barkan

Another approach which parameterizes the selector.

另一种参数化选择器的方法。

This returns all 3 options:

这将返回所有 3 个选项:

$$("ul.dropdown-menu>li:eq a")

This adds a parameter to select which in the list you want:

这将添加一个参数以选择您想要的列表中的哪个:

$$("ul.dropdown-menu>li:nth-child(1) a")

Then you can map for your tests the child number and pass it into the selector:

然后,您可以为您的测试映射子编号并将其传递给选择器:

1 = whatever
2 = whatever
3 = whatever

public void By someDroplist(String selection)
  {
return By.cssSelector("ul.dropdown-menu>li:nth-child(" + selection + ") a");
  }