如何使用 java 在 selenium 网络驱动程序中一一检查所有复选框?
How to check all check-boxes one by one in selenium web driver with the java?
我正在使用下面的代码,测试用例没有失败,但代码没有选中复选框。
@Test(priority=11)
public void Test_CheckBox_Check()throws InterruptedException {
List<WebElement> els = driver.findElements(By.xpath("//md-checkbox[@aria-checked='false']"));
System.out.println(Integer.toString(els.size()));
for ( WebElement el : els ) {
el.click();
}
}
您使用的定位器可能是导致问题的原因,请尝试以下操作:
//div[@class='ng-scope flex-20']//following::md-checkbox**[@role='checkbox']**
如果md-checkbox标识的元素都是复选框,可以省略星号部分。
下面的代码在另一种情况下对我有用:
List<WebElement> checkboxes = driver.findElements(By.xpath("//div[@class='control-group']//following::input[@type='checkbox']"));
for(WebElement check:checkboxes){
check.click();
}
添加一些后等待它的工作
@Test(priority=11)
public void Test_CheckBox_Check()throws InterruptedException {
Thread.sleep(2000);
List<WebElement> els = driver.findElements(By.xpath("//md-checkbox/div/div[@class='md-icon']"));
System.out.println(Integer.toString(els.size()));
for ( WebElement el : els ) {
Thread.sleep(2000);
el.click();
System.out.println(el.getText());
driver.findElement(By.xpath("//div[@class='col-xs-1']")).click();
}
我正在使用下面的代码,测试用例没有失败,但代码没有选中复选框。
@Test(priority=11)
public void Test_CheckBox_Check()throws InterruptedException {
List<WebElement> els = driver.findElements(By.xpath("//md-checkbox[@aria-checked='false']"));
System.out.println(Integer.toString(els.size()));
for ( WebElement el : els ) {
el.click();
}
}
您使用的定位器可能是导致问题的原因,请尝试以下操作:
//div[@class='ng-scope flex-20']//following::md-checkbox**[@role='checkbox']**
如果md-checkbox标识的元素都是复选框,可以省略星号部分。
下面的代码在另一种情况下对我有用:
List<WebElement> checkboxes = driver.findElements(By.xpath("//div[@class='control-group']//following::input[@type='checkbox']"));
for(WebElement check:checkboxes){
check.click();
}
添加一些后等待它的工作
@Test(priority=11)
public void Test_CheckBox_Check()throws InterruptedException {
Thread.sleep(2000);
List<WebElement> els = driver.findElements(By.xpath("//md-checkbox/div/div[@class='md-icon']"));
System.out.println(Integer.toString(els.size()));
for ( WebElement el : els ) {
Thread.sleep(2000);
el.click();
System.out.println(el.getText());
driver.findElement(By.xpath("//div[@class='col-xs-1']")).click();
}