Friday, 12 July 2013

How can we automate pagination using Selenium Webdriver? Need to click on Next and Previous buttons to iterate through the table.


List<webElement> pagination =driver.findElemnts(By.xpath("//div[@class='nav-pages']//a")); 
// checkif pagination link exists 

if(pagination .size()>0){ 
sop("pagination exists"); 

// click on pagination link 

for(int i=0; i<pagination .size(); i++){ 
pagination.get(i).click(); 

} else { 
sop("pagination not exists"); 


hope it'll help you :) any issue, plz let me knw.

7 comments:

  1. Hie Bramha;
    I am getting Error message:
    StaleElementReferenceException: : Element not found in the cache - perhaps the page has changed since it was looked up
    Command duration or timeout: 2.16 seconds

    Kindly find Proggrame.
    import java.io.IOException;
    import java.util.List;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;



    public class ShopcluePag {



    public static void main(String[] args) throws IOException, InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.shopclues.com/");
    //Click on Computers
    driver.findElement(By.xpath("//div[text()='Computers']")).click();
    //Click On View All
    driver.findElement(By.xpath("(//a[@href='computers/laptops.html' and text()=' View All'])[1]")).click();

    //List pagination =(List) driver.findElements(By.xpath("//a[@name='pagination'][2]"));
    List pagination =(List) driver.findElements(By.xpath("//div[@class='pagination cm-pagination-wraper center']//a"));

    pagination.size();
    System.out.println(pagination.size());
    if(pagination .size()>0){
    System.out.println("pagination exists");

    // click on pagination link

    for(int i=0; i<pagination.size(); i++){
    pagination.get(i).click();


    }
    } else {
    System.out.println("pagination not exists");
    }
    }
    }

    ReplyDelete
  2. Sorry for The late reply Mr Ajay, Try this code once.............

    package learing;

    import java.io.IOException;
    import java.util.List;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;


    public class ShopcluePag {

    public static void main(String[] args) throws IOException, InterruptedException {
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.shopclues.com/");
    driver.manage().window().maximize();
    //Click on Computers
    driver.findElement(By.xpath("//div[text()='Computers']")).click();
    //Click On View All
    driver.findElement(By.xpath("(//a[@href='computers/laptops.html' and text()=' View All'])[1]")).click();

    List pagination =driver.findElements(By.xpath("//div[@class='pagination cm-pagination-wraper center']//a"));
    int size= pagination.size();
    System.out.println(pagination.size());
    if(size>0)
    {
    System.out.println("pagination exists");
    // click on pagination link
    for(int i=2; i<size; i++){

    try{
    driver.findElement(By.xpath("//div[@class='pagination cm-pagination-wraper center']//a["+i+"]")).click() ;
    Thread.sleep(5000);
    System.out.println("Loop "+i);
    }
    catch(Exception e){
    e.printStackTrace();
    }
    }
    driver.quit();
    }
    else
    {
    System.out.println("pagination not exists");
    }
    }

    }

    ReplyDelete
  3. Hello , i tryed above code with some modification , to read data from application data grid on all pages (Number of pages 14 ) , but it showing an error after reading PAGE 2 error is ass folllwoed

    Error Message : java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at test.indigotools.Nasdaqe_InvesterListData.NasdaqeUntitled(Nasdaqe_InvesterListData.java:92)

    My code is ::

    try
    {
    /*for (int i = 1; i < s.getRows(); i++)
    {
    //Read data from excel sheet
    String s1 = s.getCell(0,i).getContents();*/

    JavascriptExecutor jsedownsmall = (JavascriptExecutor)driver;
    jsedownsmall.executeScript("scroll(0, 950)");

    List allpages = driver.findElements(By.xpath("html/body/div[4]/div[2]/div[9]/form/div[15]/div/ul/ul/li"));
    System.out.println(allpages.size());

    if(allpages.size()>0)
    {
    System.out.println("Pagination exists");
    //locator for next page button

    List nextPage = driver.findElements(By.id("quotes_content_left_lb_NextPage"));
    for(int i=0; i<=allpages.size(); i++)
    {

    System.out.println("Current Loop value "+allpages.size());
    WebElement Webtable =driver.findElement(By.id("secfilingContainer"));
    if(Webtable.isDisplayed())
    {
    // Replace TableID with Actual Table ID or Xpath
    List TotalRowCount=Webtable.findElements(By.tagName("tr"));
    System.out.println("No. of Rows in the WebTable: "+TotalRowCount.size());

    // Now we will Iterate the Table and print the Values
    int RowIndex=0;
    for(WebElement rowElement:TotalRowCount)
    {
    List TotalColumnCount=rowElement.findElements(By.xpath("td[3]"));
    int ColumnIndex=1;
    for(WebElement colElement:TotalColumnCount)
    {
    System.out.println("Row "+RowIndex+" Column "+ColumnIndex+" Data "+colElement.getText());
    ColumnIndex=ColumnIndex+1;
    }
    //if(RowIndexTotalRowCount)
    RowIndex=RowIndex+1;
    }
    Thread.sleep(3000);
    System.out.println(i);
    nextPage.get(i).click();
    System.out.println(i);
    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    //List allpages1 = driver.findElements(By.xpath("html/body/div[4]/div[2]/div[9]/form/div[15]/div/ul/ul/li"));
    //}
    }
    else
    {
    System.out.println("Tab Heading is not displayed ");
    }
    }
    }
    else
    {
    System.out.println("Tab Heading is not displayed ");
    }
    }

    catch(Exception e){
    e.printStackTrace();
    }
    }

    ReplyDelete
  4. It is really helpful !An opportunity to read a fantastic and imaginary blogs.It gives me lots of pleasure and interest.
    sap abap online training india

    ReplyDelete
  5. Hi Brahma,

    I need to do pagination with page object Model. No idea how to start. Can you share an example?

    ReplyDelete

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...