[英]Check if a checkbox is checked or not using Selenium
The following code snippet represents a checkbox
and I want to check whether it is checked or not using Selenium.以下代码片段代表一个
checkbox
,我想检查是否使用 Selenium 选中它。
for (int j = 2; j < 5; j++)
{
List<WebElement> td1 = row1.findElements(By.tagName("td"));
WebElement check = td1.get(j);
if (check.isEnabled())
{
if (!check.isSelected())
{
List<WebElement> checkBox = ele
.findElements(By.xpath("//input[contains(@ng-change,'vm.updateInvoicingMethodMailByGroup(item)')]"));
WebElement elem3 = checkBox.get(count);
elem3.click();
count++;
break;
}
}
}
When I check this, the class changes from当我检查这个时,班级从
class="ng-valid ng-dirty ng-valid-parse ng-empty ng-touched"
to到
class="ng-valid ng-dirty ng-valid-parse ng-not-empty ng-touched"
HTML HTML
<input type="checkbox" name="invoicingByMailFlag" ng-model="item.invoicingByMailFlag"
ng-change="vm.updateInvoicingMethodMailByGroup(item)" class="ng-valid ng-dirty
ng-valid-parse ng-empty ng-touched">`
You can check if it has class attribute ng-empty
or not您可以检查它是否具有类属性
ng-empty
List<WebElement> checkBoxes = ele.findElements(By.xpath("//input[contains(@ng-change,'vm.updateInvoicingMethodMailByGroup(item)')]"));
WebElement checkBox = checkBoxes.get(count);
if (checkBox.getAttribute("class").contains("ng-empty")) {
// checkbox not checked
}
If your requirement to check the unchecked boxes then try below code :如果您需要选中未选中的框,请尝试以下代码:
List <WebElement> allcheckbox = driver.findElements(By.xpath("//input[@type='checkbox']"));
for(WebElement element : allcheckbox)
{
if(!(element.isSelected()))
{
element.click();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.