Selenium WebDriver和下拉框

43

如果我想要选择下拉框中的一个选项,有几种方法可以做到这一点。我经常使用的是:

driver.findElement(By.id("selection")).sendKeys("Germany");

但那并不总是有效。有时会选择另一个选项。所以我上网搜了一下,找到了这段代码,它每次都能工作:

WebElement select = driver.findElement(By.id("selection"));
    List<WebElement> options = select.findElements(By.tagName("option"));
    for (WebElement option : options) {
        if("Germany".equals(option.getText()))
            option.click();
    }

但是那样做非常慢。如果我有一个很长的列表,其中有很多项,那么它确实需要太多时间。因此我的问题是,是否有一种每次都能快速运行的解决方案?

10个回答

50

你可以尝试这样做:

IWebElement dropDownListBox = driver.findElement(By.Id("selection"));
SelectElement clickThis = new SelectElement(dropDownListBox);
clickThis.SelectByText("Germany");

25
我认为这是一些C#代码吧?但它帮助我找出了以下代码:WebElement dropDownListBox = driver.findElement(By.id("selection")); Select clickThis = new Select(dropDownListBox); clickThis.selectByValue("Germany");速度快多了!谢谢! - tester
2
IWebElement和SelectElement应该导入哪个包? - Ripon Al Wasim
1
我在哪里可以找到SelectElement类? - amhed
2
请注意,在 C# 中,SelectElement 是一个不同的 NuGet 项目中的一部分,称为 Selenium.Support。 - Luis Perez

25

请尝试以下方法:

import org.openqa.selenium.support.ui.Select;

Select droplist = new Select(driver.findElement(By.Id("selection")));   
droplist.selectByVisibleText("Germany");

它的Scala实现是什么?谢谢。 - wei

4

尝试使用Select帮助类,看看是否有任何差异?

String valueToSelect= "Germany";
WebElement select = driver.findElement(By.id("selection"));
Select dropDown = new Select(select);           
String selected = dropDown.getFirstSelectedOption().getText();
if(selected.equals(valueToSelect)) {//do stuff already selected}
List<WebElement> Options = dropDown.getOptions();
for(WebElement option:Options){
  if(option.getText().equals(valueToSelect)){
       option.click();  
  }
}

1
抱歉,这就像我的解决方案一样慢。 - tester

2

由于某种奇怪的原因,webdriver(版本2.25.1.0)的 SelectElement 与firefoxdriver(Firefox 15)不兼容。有时它可能无法从下拉列表中选择选项。但是,它似乎可以与chromedriver一起使用... 这个链接 是 chromedriver 的下载链接... 只需将其放入bin目录即可。


1
只需按照以下示例将您的WebElement包装到Select对象中即可。
Select dropdown = new Select(driver.findElement(By.id("identifier")));

一旦完成此操作,您可以通过3种方式选择所需的值。考虑像这样的HTML文件。
<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>

现在要识别下拉菜单,请执行以下操作:
Select dropdown = new Select(driver.findElement(By.id("designation")));

选择“程序员”选项的方法如下:

说出“程序员”即可

dropdown.selectByVisibleText("Programmer ");

或者

 dropdown.selectByIndex(1);

或者

 dropdown.selectByValue("prog");

快乐编程 :)


1

从下拉列表中选择选项的示例:

通过使用id、csspath、xpath或name单击下拉列表。在此示例中,我使用了id。

driver.findElement(By.id("dropdownlistone")).click(); // To click on drop down list
driver.findElement(By.linkText("india")).click(); // To select a data from the drop down list.

0
public static void mulptiTransfer(WebDriver driver, By dropdownID, String text, By to)
{   
    String valuetext = null;
    WebElement element = locateElement(driver, dropdownID, 10);
    Select select = new Select(element);
    List<WebElement> options = element.findElements(By.tagName("option"));
    for (WebElement value: options) 
    {
        valuetext = value.getText();
        if (valuetext.equalsIgnoreCase(text))
        {
            try
            {
                select.selectByVisibleText(valuetext);
                locateElement(driver, to, 5).click();                           
                break;
            }
            catch (Exception e)
            {
                System.out.println(valuetext + "Value not found in Dropdown to Select");
            }       
        }
    }
}

0

我必须努力寻找如何实现特别是那些对这个工具新手(比如我)

C# 代码:

IWebElement ddl = ffDriver.FindElement(By.Id("ddlGoTo")); 
OpenQA.Selenium.Support.UI.SelectElement clickthis = new OpenQA.Selenium.Support.UI.SelectElement(ddl);
clickthis.SelectByText("Your Text");

希望这能帮到其他人!

0
select = driver.FindElement(By.CssSelector("select[uniq id']"));
                selectElement = new SelectElement(select);
                var optionList =
                    driver.FindElements(By.CssSelector("select[uniq id']>option"));
                selectElement.SelectByText(optionList[GenerateRandomNumber(1, optionList.Count())].Text);

-2

你可以使用这个

(new SelectElement(driver.FindElement(By.Id(""))).SelectByText("");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接