java - 我需要验证应用程序的多个页面中是否存在相同的元素

标签 java selenium-webdriver

我的情况是我需要检查应用程序中的所有页面中是否存在按钮。

在我的第一页中有 20 个这样的元素,我获取了大小,如果大小为 > 0 则测试通过。同样,如果我有 5 个这样的页面,并且元素的预期总数为 96,那么在所有前四页中,该计数将为 20,在最后一页中,该计数将为 16。

我需要所有元素的总数并与总数进行比较。

我尝试过 for 循环但不起作用

String pageNumberText = objects.pageNum().getText();
String lastWord = pageNumberText.substring(pageNumberText.lastIndexOf(" ") + 1);
System.out.println(lastWord); int pageNumb = Integer.parseInt(lastWord);
for (int i = 1; i <= pageNumb; i++) {
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='tgbtn' and @class='tgbtn mainPriceContainer']")));
    System.out.println(pageNumberText);

    List<WebElement> dealBtn = driver.findElements(By.xpath("//*[@id='tgbtn']/a"));
    if (dealBtn.size() > 0) {
        Assert.assertTrue(true, "Deal button is present");
        System.out.println("Button verification of page " + i + " successful");
        System.out.println("No of deal buttons present are : " + dealBtn.size());
        objects.nextPageBtn().click();
    }
}

我得到的结果甚至在导航到下一页之前,结果正在打印

5
1 of 5
Button verification of page 1 successful
No of deal buttons present are : 20
1 of 5
Button verification of page 2 successful
No of deal buttons present are : 20
1 of 5
Button verification of page 3 successful
No of deal buttons present are : 20
1 of 5
Button verification of page 4 successful
No of deal buttons present are : 20
1 of 5
Button verification of page 5 successful
No of deal buttons present are : 20

结果有误 最后一页的元素数量只有16个

加载第二页时操作停止

最佳答案

我相信您的问题在于等待条件:

   wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='tgbtn' and @class='tgbtn mainPriceContainer']")));

问题是这个元素存在于您的每个页面上,因此当您单击“NextPageBtn”时,条件立即满足,并且执行不会等到下一页实际加载,因此所有 5 个页面都存在在加载下一页之前执行速度非常快,所有 (20) 个结果都是来自第一页的结果。

您应该尝试的是等待,直到分页元素中的当前页面的编号等于i,或者类似的内容,这表明您实际上已经到达了循环期望的页面在。

关于java - 我需要验证应用程序的多个页面中是否存在相同的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58687849/

相关文章:

java - 获取表大小时如何排除表的第一行和最后一行

java - 当前 View 不支持使用 Qt WebDriver 和 Selenium 的 Qml 应用程序的此命令

java - 我可以在 TestNG 测试用例上指定一个类范围的组吗?

具有功能接口(interface)参数类型的 Java 通用方法

java - NoClassDefFound错误: org/dom4j/io/SAXReader

i 框架的 Selenium 问题

java - 如何使用 Selenium 关闭 Chrome 的下载对话框(通过单击取消)

java - 无法单击复选框以取消选中它似乎已隐藏

java - java while循环中无法到达的语句

java - 如何计算数组列表中特定类的实例数?