我需要找到一个按钮,可以选择也可以不选择。
我首先找到按钮的xpath,检查是否已选中,此检查失败"无法找到元素:{"方法":"标记名称&#34 ;,"选择器":"输入"}"
我的布尔线检查应该是什么?
WebElement element = driver.findElement(By.xpath("//i[contains(@class,'i-icon--plus-18-movie')]"));
boolean isChecked = element.findElement(By.tagName("input")).isSelected();
if(isChecked){
System.out.println("is checked");
element.click();
}
<age-question-button class="ng-scope ng-isolate-scope" state="qc.answer[question.name]" icon="plus-18-movie" active-text="The Customer is over 18" inactive-text="The Customer is NOT over 18" ng-repeat="question in qc.questionsList track by question.name" audit="cc.utils.audit(qc.answer[question.name] ? question.auditInactive : question.auditActive)">
<label class="c-option u-p-0 u-ml-md u-pull-left" tooltip-append-to-body="true" uib-tooltip="The Customer is NOT over 18">
<input class="ng-untouched ng-valid ng-dirty ng-valid-parse" ng-model="state" ng-change="audit()" style="" type="checkbox">
<i class="c-option__button i-icon i-icon--plus-18-movie"></i>
</label>
</age-question-button>
答案 0 :(得分:2)
请尽量避免同一路径中的多个班级。 你可以使用如下
WebElement elem = driver.findElement(By.className("ng-isolate-scope"));
WebElement element = elem.findElement(By.className("ng-valid-parse"));
boolean isChecked = element.findElement(By.tagName("input")).isSelected();
if(isChecked){
System.out.println("is checked");
element.click();
}
如果您有多个具有相同名称的类,则可以使用该元素的列表和所需索引。
WebElement elem = driver.findElement(By.className("ng-isolate-scope"));
List<WebElement> element = elem.findElements(By.className("ng-valid-parse"));
boolean isChecked = element.get(indexOfyourdesiredELement).findElement(By.tagName("input")).isSelected();
if(isChecked){
System.out.println("is checked");
element.get(indexOfyourdesiredELement).click();
}
您也可以写
boolean isChecked = element.findElement(By.cssSelector("input[type='checkbox']")).isSelected();
编辑:以下是更新后的解决方案。如果选中它,它将单击复选框。你可以自己扭转逻辑。
WebElement element = driver.findElement(By.tagName("age-question-button"));
boolean isChecked = element.findElement(By.cssSelector("input[type='checkbox']")).isSelected();
if(isChecked){
System.out.println("is checked");
element.click();
}
如果有多个年龄问题按钮问题,请按照此说明进行操作。
List<WebElement> element = driver.findElements(By.tagName("age-question-button"));
for(int i = 0 ; i< element.size() ; i++){
boolean isChecked = element.get(i).findElement(By.cssSelector("input[type='checkbox']")).isSelected();
if(isChecked){
System.out.println("is checked and it will click on checkbox if it is checked");
element.get(i).click();
}
}
答案 1 :(得分:0)
尝试使用cssSelector urllib.request
input[type='checkbox']