user2376425
user2376425

Reputation: 85

Search an element in all pages in Selenium WebDriver (Pagination)

I need to search for particular text in a table on all the pages. Say i have got to search for text (e.g : "xxx") and this text is present at 5th row of table on 3rd page. I have tried with some code :

List<WebElement> allrows = table.findElements(By.xpath("//div[@id='table']/table/tbody/tr"));
List<WebElement> allpages = driver.findElements(By.xpath("//div[@id='page-navigation']//a"));
    System.out.println("Total pages :" +allpages.size());
    for(int i=0; i<=(allpages.size()); i++)
        {
            for(int row=1; row<=allrows.size(); row++)
                {
                    System.out.println("Total rows :" +allrows.size()); 
                    String name = driver.findElement(By.xpath("//div[@id='table']/table/tbody/tr["+row+"]/td[1]")).getText();
                    //System.out.println(name);
                    System.out.println("Row loop");
                    if(name.contains("xxxx"))
                        {
                            WebElement editbutton = table.findElement(By.xpath("//div[@id='table']/table/tbody/tr["+row+"]/td[3]"));
                            editbutton.click();
                            break;
                        }
                    else
                    {
                        System.out.println("Element doesn't exist");
                    }
                    allpages = driver.findElements(By.xpath("//div[@id='page-navigation']//a"));
                }

            allpages = driver.findElements(By.xpath("//div[@id='page-navigation']//a"));
            driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
            allpages.get(i).click();
        }

Sorry, i missed to describe the error. Well this code gets executed properly, it checks for element "xxx" on each row of every page and clicks on editbutton when its found.

After that it moves to "allpages.get(i).click();" // code is for click on pages

But its unable to find any pagination, so it displays error of "Element is not clickable at point (893, 731). Other element would receive the click...."

Upvotes: 0

Views: 10755

Answers (1)

Furious Duck
Furious Duck

Reputation: 2019

For every page loop you use one table WebElement object. So I assume that after going to the next page you get StaleElementReferenceException. I guess the solution could be with defining table on every page loop. Move this line List<WebElement> allrows = table.findElements(By.xpath("//div[@id='table']/table/tbody/tr")); after for(int i=0; i<=(allpages.size()); i++) too

EDIT: And, btw, at this line allpages.get(i).click() I think you must click the next page link, not the current one as it seems to be

Upvotes: 0

Related Questions