简体   繁体   中英

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)

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();
            }
          }               
      }

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM