2011-08-29 59 views
36

如果我想选择一个下拉框选项,有几种方法可以做到这一点。我总是习惯:Selenium WebDriver和DropDown Boxes

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

但没有奏效每次。有时选择另一个选项。所以我用google搜索了一下,发现这段代码每次都有效:

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(); 
    } 

但是这样做的确很慢。如果我有一个很长的名单,其中有很多项目,它真的需要太多时间。所以我的问题是,是否有解决方案,每次都有效,速度很快?

回答

46

你可以试试这个:

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

我认为这是一些C#代码的东西?但它帮助我找出以下代码: WebElement dropDownListBox = driver.findElement(By.id(“selection”)); \t \t选择clickThis = new Select(dropDownListBox); \t \t clickThis.selectByValue(“Germany”); 快得多!谢谢! – tester

+2

应该为IWebElement和SelectElement导入哪个包? –

+0

谢谢测试人员,该代码适用于硒2。 –

3

尝试选择辅助类,看看是否有什么差别?

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

sry,这和我的解决方案一样慢。 – tester

23

尝试以下操作:

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

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

是的,上面的代码对我来说效果不错 –

+0

它的scala实现是什么?谢谢 – Way

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"); 

希望这有助于他人!

2

由于一些奇怪的原因,用于webdriver(版本2.25.1.0)的SelectElement未正确使用firefoxdriver(Firefox 15)。有时它可能不会从下拉列表中选择一个选项。但是,它确实与chromedriver一起工作... This是一个链接到chromedriver ...只需将它放入bin目录即可。

从下拉列表中选择一个选项
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
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); 
+0

它工作完美。 – Emebet

0

例子:

点击使用ID或csspath或XPath或名称下拉列表中。我在这里使用了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. 
-2

您可以使用此

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

只是包装你的WebElement为选择对象如下图所示

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"); 

编码快乐:)