【问题标题】:How to check all check-boxes one by one in selenium web driver with the java?java - 如何使用Java一一检查selenium Web驱动程序中的所有复选框?
【发布时间】:2017-04-10 10:50:54
【问题描述】:

我正在使用下面的代码,测试用例没有失败,但代码没有检查复选框。

@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上面展开吗?一个具有类值 - 'md-container md-ink-ripple'。我认为复选框在这个 div 里面。
  • 请查看更新后的屏幕截图。
  • 你可以尝试点击这个div或者里面的那个吗?不完全确定,但我认为它应该有效。
  • 检查是否需要等待这些元素可见。似乎您可以在浏览器的 xpath 检查器中获取它们。你能在浏览器中运行带有内部 div 的最新 xpath 并检查出现了什么吗?
  • 它只显示复选框吗?尝试明确等待这些元素的可见性,然后再找到它们。

标签: java selenium xpath selenium-webdriver selenium-firefoxdriver


【解决方案1】:

您使用的定位器可能是导致问题的原因,请尝试以下操作:

//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();
        }

【讨论】:

  • FAILED: Test_CheckBox_Check org.openqa.selenium.InvalidSelectorException: The given selector //div[@class='ng-scope flex-20']//following::md-checkbox**[@ role='checkbox']** 无效或不会生成 WebElement。发生以下错误: InvalidSelectorError: Unable to locate an element with the xpath expression //div[@class='ng-scope flex-20']//following::md-checkbox**[@role='checkbox'] ** 因为以下错误:TypeError: The expression cannot be convert to return the specified type.
  • 以上异常即将到来。请看一下并帮助我。
【解决方案2】:

添加一些等待它的工作后

@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();

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 2014-08-31
    相关资源
    最近更新 更多