2014-01-29 88 views
1

目前正在硒的webdriver和使用的Java選擇多選框元素。我想知道多選框中選擇值。選項已被選中。如果我想選擇任意兩個或更多選項。如何執行該操作。如何在硒的webdriver

的HTML是如下:

<select id="swpacksId" multiple="" style="width: 125px; display: none;" name="swPacks[]"> 
<option selected="" value="ADVIP">ADVIP</option> 
<option selected="" value="ADVLEG">ADVLEG</option> 
<option selected="" value="ADVSEC">ADVSEC</option> 
<option selected="" value="Boot">Boot</option> 
<option selected="" value="H323">H323</option> 
<option selected="" value="IBC">IBC</option> 
<option selected="" value="MULTI">MULTI</option> 
<option selected="" value="None">None</option> 
</select> 

enter image description here

回答

4

在函數傳值的列表中使用任何分隔符可以說逗號作爲分隔符:

public static void selectMultipelValues(String multipleVals) { 
    String multipleSel[] = multipleVals.split(","); 

    for (String valueToBeSelected : multipleSel) { 
     new Select(driver.findElement(By.id(propId))).selectByVisibleText(valueToBeSelected); 
     driver.findElement(By.id(ddObj)).sendKeys(Keys.CONTROL); 
    } 
} 
1

請檢查下面的網址可以幫助你

http://selenium.polteq.com/en/controlling-a-selectbox-or-dropdownbox-with-selenium-webdriver/

您可以檢查以下選項

public void selectByValue() { 
    Select selectBox = 
      new Select(driver.findElement(By .cssSelector("select#id_contact")));  
    selectBox.selectByValue("2"); 
} 

public void selectByIndex() { 
    Select selectBox = 
      new Select(driver.findElement(By.cssSelector("select#id_contact"))); 
    selectBox.selectByIndex(2); 
} 

你可以根據你的要求改變它

-2

我已經寫了這樣的代碼..第一我取消選擇多選擇框中的所有值,然後我選擇我想要的值..它工作正常..

Log.info("Clicking on Softwarepack dropdown"); 
JavascriptExecutor executor31 = (JavascriptExecutor)driver; 
executor31.executeScript("document.getElementById('swpacksId').style.display='block';"); 
Select select31 = new Select(driver.findElement(By.id("swpacksId"))); 
select31.deselectAll(); 
select31.selectByVisibleText("ADVLEG"); 
Thread.sleep(6000); 
JavascriptExecutor executor32 = (JavascriptExecutor)driver; 
executor32.executeScript("document.getElementById('swpacksId').style.display='block';"); 
Select select32 = new Select(driver.findElement(By.id("swpacksId"))); 
select32.selectByVisibleText("SIP"); 
2

如果你有Utils這樣的靜態方法:

public static void selectTheDropDownList(WebElement dropDown,String text) 
{ 
    Select select = new Select(dropDown); 
    select.selectByVisibleText(text);  
} 

,你可以像這樣,可以選擇多個選項:

Utils.selectTheDropDownList(dropDown,text1); 
Utils.selectTheDropDownList(dropDown,text2); 
. . . 
Utils.selectTheDropDownList(dropDown,textn); 

這應該起作用。

-1
new Select(driver.findElementByXPath("XXXXXXXXXXX"))).selectByIndex(2); 
+0

以上可以使用,如果你有一個下拉/組合框。索引從0開始。 – Vishal

+1

這不回答這個問題。他詢問了多重選擇,你的答案只適用於單一選擇。 – Smeiff

1

您需要單擊元素與控制。 這裏是文檔如何使這種行動 https://code.google.com/p/selenium/wiki/AdvancedUserInteractions

在我們的情況下,它可能是:

Select select = new Select(element); 

Actions builder = new Actions(driver); 
builder.keyDown(Keys.CONTROL) 
.click(select.getOptions().get(2)) 
.keyUp(Keys.CONTROL); 

builder.build().perform(); 
0

爲多個部分,你可以這樣做:

Select select = new Select(element); 
Actions builder = new Actions(driver); 
builder.keyDown(Keys.CONTROL) 
.click(select.getOptions().get(2)) 
.click(select.getOptions().get(3)) 
.click(select.getOptions().get(4)) 
.keyUp(Keys.CONTROL); 

builder.build().perform(); 
2

這個工作對我來說:

final String[] textOptions = {"value1", "value2"}; 
final WebElement element = driver.findElement(By.id("someId")); 
final Select dropdown = new Select(element); 
final List<WebElement> options = dropdown.getOptions(); 
final Actions builder = new Actions(driver); 
final boolean isMultiple = dropdown.isMultiple(); 
if (isMultiple) { 
    dropdown.deselectAll(); 
} 
builder.keyDown(Keys.CONTROL); 
for (String textOption : textOptions) { 
    for (WebElement option : options) { 
     final String optionText = option.getText().trim(); 
     if (optionText.equalsIgnoreCase(textOption)) { 
      if (isMultiple) { 
       if (!option.isSelected()) { 
        builder.click(option); 
       } 
      } else { 
       option.click(); 
      } 
      break; 
     } 
    } 
} 
builder.keyUp(Keys.CONTROL).build().perform(); 
0

使用下面的代碼,它的簡單和容易!它爲我工作。

Select dd1 = new Select(driver.findElement(By.name("swPacks[]"))); 
dd1.selectByVisibleText("ADVIP"); 
dd1.selectByVisibleText("ADVLEG");