我需要在我的应用程序的多个页面中验证同一元素的存在

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

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

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

我曾尝试过循环,但无法正常工作

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个

加载第二页后动作停止

qq8117513 回答:我需要在我的应用程序的多个页面中验证同一元素的存在

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

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

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

您应该尝试的是等到分页元素中的当前页面数等于i或类似的值,表明您实际上已经到达了循环所希望的页面。 / p>

,

在这里,我可以添加两点来修正您的代码: 1)如果需要在循环结束时将计数设为'96',请在for循环外部声明List<WebElement> dealBtn,在for循环内部创建另一种相同类型的变量,并分配由此{{1 }}到该局部变量。稍后使用for循环或其他Collections概念,将局部List<WebElement>元素附加到全局变量'dealBtn'中。通过这种方式,我们可以在for循环结束时获得总数96。

2)等待元素: for循环的第一行将尝试等待直到元素可见为止。因此,您可以在driver.findElements(By.xpath("//*[@id='tgbtn']/a"))之后的if语句内添加另一个等待条件,以使用'invisibilityOfElementLocated()'等待同一元素的不可见性。通过这种方式,一旦您单击nextPageButton(),它将等待直到元素不可见。一旦页面开始重新加载,元素将从DOM中消失,并且满足等待条件。稍后它将进入循环的第二轮,并等待直到页面在DOM中包含该元素。这样,将重复执行相同的操作,直到循环终止。...

通过结合这两种情况,希望您的脚本将在所有5页上执行,最后,您的元素总数为96(在循环外打印总数)

尝试以下代码:

List<WebElement>
本文链接:https://www.f2er.com/3168854.html

大家都在问