簡體   English   中英

如何在Java中使用Selenium WebDriver從多個選擇框中選擇一個復選框?

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

我在網頁中具有以下用於多重選擇框的代碼,我需要使用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>

有人可以建議如何選擇我選擇的選項組嗎?

為了切換一個復選框,就像單擊它一樣簡單。

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

您可能還希望檢查是否已選中一個復選框: WebElement#isSelected

例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM