Home > Software engineering >  if the next page xpath is displayed, click it and count the elements of the next page, and continue
if the next page xpath is displayed, click it and count the elements of the next page, and continue

Time:10-01

I have made a unique xpath for enabledNextPage. it is not found on the UI as long as the next page arrow is disabled. but on while loop it is giving me error that the element not found, which is correct that i wasn't found and it should not continue with the while loop. I used "try catch" and if the next page element is not present it still runs the while loop and gives me error on the next line which is to click it.

List<WebElement> rows = new ArrayList();
rows.addAll(driver.findElements(By.xpath("//*[@class='sortable-row']")));

    while (schedulingModel.nextPageEnabled.isDisplayed())
    {
        nextPageEnabledXpath.click();
        Thread.sleep(3000);
        rows.addAll(driver.findElements(By.xpath("//*[@class='sortable-row']")));
       
    }

CodePudding user response:

I would rather have a different logic implemented by findElements, this will return me a list of next page arrow or link, if the size if >0 then it must have next page link, if not, well then no next page link.

List<WebElement> nextPageList = driver.findElements(By.xpath("xpath of next page arrow"));
if (nextPageList.size() > 0) {
    System.out.println("Next page link is present");
    // code to click on it, or count or whatever.
}
else {
    System.out.println("Next page link is not present");
}

CodePudding user response:

you can try with findElements instead using isDisplayed as it won't throw any exception if element is not present. it will be easy to share logic if you can share more details about your query like URL...

List<WebElement> rows = new ArrayList();
rows.addAll(driver.findElements(By.xpath("//*[@class='sortable-row']")));

while (driver.findElements(By.xpath("nextPageEnabled xpath value").size()>0)
{
    nextPageEnabledXpath.click();
    Thread.sleep(3000);
    rows.addAll(driver.findElements(By.xpath("//*[@class='sortable-row']")));
   
}
  • Related