pinki talukdar
pinki talukdar

Reputation: 47

How to search an element in a table using pagination in selenium Web Driver with java

Hi i am facing problem in my script.I need to verify whether an element is present in the pagination table or not. I write the below code but it is showing error and also loop is not working.Can anyone help me in my script? This is my code and the error what i am getting:

//pagination of the table

    List<WebElement> allpages=d.findElements(By.xpath("//div[@id='reportPagination_wrapper']//a"));
    System.out.println("Total page : "+allpages.size());

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

        //click on pagination link

        for(int i=0;i<=allpages.size();i++)
        {
            if(allpages.contains(flight_no))
            {
                System.out.println("record exists");

                break;
            }

            else
            {
                System.out.println("eleenmt no");
                allpages.get(i).click();
            }
            }               
        }

        }

Output: Total page : 13

   pagination exist

   eleenmt no
   eleenmt no

FAILED: search_basedon_flightno

The error: org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=71.0.3578.98)

Upvotes: 0

Views: 1979

Answers (1)

Sameer Arora
Sameer Arora

Reputation: 4507

StaleElementReferenceException comes when the element is no longer available in the dom or has become stale. In order to correct it, you need to again fetch the elements and then operate on it.
For example in your case it should be:

List<WebElement> allpages=d.findElements(By.xpath("//div[@id='reportPagination_wrapper']//a"));
    System.out.println("Total page : "+allpages.size());

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

        //click on pagination link

        for(int i=0;i<=allpages.size();i++)
        {
            allpages=d.findElements(By.xpath("//div[@id='reportPagination_wrapper']//a"));
            if(allpages.contains(flight_no))
            {
                System.out.println("record exists");

                break;
            }

            else
            {
                System.out.println("eleenmt no");
                allpages.get(i).click();
            }
          }               
      }

  }

Upvotes: 1

Related Questions