简体   繁体   中英

How to select a checkbox from a multiple select box using selenium webdriver in java?

I have the below code for the multiple select box in the web page and I need to make a selection of any option group for the list using selenium webdriver

<div class="ms-drop bottom" style="display: block;">
    <div class="ms-search">
        <ul style="max-height: 250px;">
            <li class="group">
                <label class="optgroup" data-group="group_0" style="display: block;">
                    <input type="checkbox" name="selectGroup">AmaWaterways
                </label>
            </li>
            <li class="multiple" style="width: 180px;">
                <label style="display: block;">
                    <input type="checkbox" data-group="group_0" value="AmaSerena" name="selectItem">AmaSerena
                </label>
            </li>
            <li class="multiple" style="width: 180px;">
                <label style="display: block;">
                    <input type="checkbox" data-group="group_0" value="AmaPura" name="selectItem">AmaPura
                </label>
            </li>
        </ul>
    </div>
</div>

Can somebody suggest how to select an option group of my choice?

In order to toggle a checkbox, it is as simple as clicking it.

String targetOption = "whatever";
By targetOptionLocator = By.cssSelector("input[type=checkbox][value=" + targetOption + "]");
driver.findElement(targetOptionLocator).click();

You may also want to check if a checkbox is already checked: WebElement#isSelected .

Eg:

void selectOption(String optionName) {
  By targetOptionLocator = By.cssSelector("input[type=checkbox][value='" +  + optionName + "']");
  WebElement option = driver.findElement(targetOptionLocator);
  if(!option.isSelected())
    option.click();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM