user2410266
user2410266

Reputation: 551

Click Next link until pagination Next action is disabled in selenium webdriver

I want to verify elements in a row inside a table in by navigating each page through pagination.I am able to navigate to each page and assert element but the probelm is at last page the loop still continues even though the Next link is grayed out.

When Next link is disabled

  <span>
  <a class="next_btn inactive">NEXT ></a>
  </span>

When Next link is enabled

   <span>
   <a class="next_btn" href="home.do?action=Next&start=10&end=20&sort=&        
   type=00&status=&fromDate=04/02/2017&toDate=05/02/2017&school=&     
   district=0009">NEXT ></a>
   </span>

Actual Code

  public void submissionType() throws Exception {
  driver.findElement(By.linkText("NEXT >"));
    while(true) {
        processPage();
        if (pagination.isDisplayed() && pagination.isEnabled()){
            pagination.click();
            Thread.sleep(100L);
        } else 
            break;

    } 
    }

  private void processPage() {
    String statusColumn="//td[@class='gridDetail'][2]";
    List<WebElement> list = table.findElements(By.xpath(statusColumn));
    for (WebElement checkbox : list) {
        String columnName=checkbox.getText();
        Asserts.assertThat(columnName,"File");  
     }
    }

Upvotes: 0

Views: 2556

Answers (2)

JeffC
JeffC

Reputation: 25714

I know you already accepted an answer but one of the statements is not correct. The locator, By.cssSelector("a.next_btn"), will find both the enabled and disabled button so it will not cause the loop to break.

Looking at your code, I would offer a few suggestions/corrections.

  1. .isEnabled() only really works on INPUT tags so testing for that doesn't really accomplish anything here.

  2. Using Thread.sleep() is not a good practice. You can google some explanations but basically the problem is that it's an inflexible wait. If the element you are waiting for becomes available in 15ms, you are still going to wait 10s or whatever your sleep is set to. Using an explicit wait (WebDriverWait) is a best practice.

I would clean up your functions and write them like

public void submissionType()
{
    WebDriverWait wait = new WebDriverWait(driver, 10);
    By nextButtonLocator = By.cssSelector("a.next_btn:not(.inactive)");
    List<WebElement> nextButton = driver.findElements(nextButtonLocator);
    while (!nextButton.isEmpty())
    {
        processPage();
        nextButton.get(0).click();
        wait.until(ExpectedConditions.stalenessOf(nextButton.get(0))); // wait until the page transitions
        nextButton = driver.findElements(nextButtonLocator);
    }
}

private void processPage()
{
    for (WebElement checkbox : table.findElements(By.xpath("//td[@class='gridDetail'][2]")))
    {
        Asserts.assertThat(checkbox.getText(), "File");
    }
}

Upvotes: 0

Anish Pillai
Anish Pillai

Reputation: 1023

Instead of identifying the element with By.linkText("NEXT >"), try identifying it with By.cssSelector("a.next_btn").

When you use this approach, then when the object becomes disabled, its class name would change, and hence your object would no longer get identified and your loop would break.

Edit: Add try block and Catch NoSuchElement exception to avoid exceptions.

Upvotes: 1

Related Questions