2011-08-29 58 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"); 

編碼快樂:)