如何在selenium webdriver中选择多选框中的元素

时间:2014-01-29 03:57:55

标签: java html selenium selenium-webdriver

目前正在使用 Selenium WebDriver 并使用 Java ..我想知道在多选框中选择值。选项已被选中..如果我想选择任何两个或更多选项。如何执行该动作。

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

11 个答案:

答案 0 :(得分:3)

在函数传递中,值列表使用任何分隔符,让逗号作为分隔符:

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 :(得分: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);

这应该有效。

答案 2 :(得分: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();

答案 3 :(得分: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); 
}

您可以根据自己的要求进行更改

答案 4 :(得分: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();

答案 5 :(得分: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();

答案 6 :(得分:0)

使用以下代码,简单易用!它对我有用。

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

答案 7 :(得分:0)

我花了很多时间尝试用Chrome浏览器的网络驱动程序按下控制键来模拟点击。经过一番调查后,发现您对多选的OPTION元素产生点击时,实际上没有点击发生。而是在浏览器中生成了一个更改事件。这就导致了这样的情况,即对多选的其他选项的后续“单击”没有清除先前选择的选项,这有时是不希望的行为。 为了解决这个问题,我提出了以下解决方案:

Actions actions = new Actions(driver);
if(controlNeeded)
    actions.keyDown(Keys.CONTROL);
actions.moveToElement((WebElement) option_you_want_to_click);
actions.clickAndHold();
actions.pause(100);
actions.release();
if(controlNeeded)
    actions.keyUp(Keys.CONTROL);
actions.build().perform();

通过这种方式,您可以根据Ctrl键选择单个和多个元素。

答案 8 :(得分:0)

在下面尝试此代码-> //https://jqueryui.com/selectable/

int i=0;<br>
Actions actObj = new Actions(driverObj);    <br>    
for(WebElement ele: selectableList){<br>
    actObj.moveToElement(ele);<br>
    actObj.keyDown(Keys.CONTROL).click(ele).build().perform();<br>
    System.out.println("selectableList["+i+"] = "+ele.getText()+" Clicked");<br>
    i++;<br>
}<br>

答案 9 :(得分:-1)

new Select(driver.findElementByXPath("XXXXXXXXXXX"))).selectByIndex(2);

答案 10 :(得分:-2)

我已经编写了这样的代码.1我取消选中多选框中的所有值然后我选择了我想要的值..它工作正常..

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