Navigating through pagination with Selenium adding items to a HashMap

19,674

You can do something like this, assuming that the link is an <a> html element you need

1 Create a List of Web elements for example <a> elements driver.findElements(By.tagName("a"))

2 Iterate List retrieved from the last step and check some meta information of the specific link that you need to click on, for example the title elements.get(i).getAttribute("title");

3 Compare if the title is the one you need if (title.equals("Next Page")) {

3 Send click signal

Here is a code snippet

new WebDriverWait(
            driver, TIME_TO_WAIT).until(
                    ExpectedConditions.presenceOfElementLocated(
                            By.tagName("a")));
List<WebElement> elements = driver.findElements(By.tagName("a"));
for (int i = 0; i < elements.size(); i++) {
    String title = elements.get(i).getAttribute("title");
    if (title.equals("Next Page")) {
        elements.get(i).click();
        break;
    }
}
Share:
19,674
Nazrod12
Author by

Nazrod12

Updated on June 04, 2022

Comments

  • Nazrod12
    Nazrod12 almost 2 years

    I have a list of WebElements on a results page that I need to extract and add to a HashMap.

    There is pagination on the webpage that looks like this: 1, 2, 3, 4, 5, Next. These are all links and when Next is clicked on, it will then navigate to Page 2 and display 6 on the pagination, whilst removing 1, and so on.

        List<WebElement> pagination = driver.findElements(By.xpath("//div[@class='pagination-container']//a"));
            int s = pagination.size();
            for(int i=0;i<=s;i++){
                this.getAuthors();
                    driver.get(Constants.url);
                    Thread.sleep(5000);
    
                pagination = driver.findElements(By.xpath("//div[@class='pagination-container']//a"));
                pagination.get(i).click();
                Thread.sleep(5000);
            }
    

    The getAuthors() method goes through the necessary elements on the page and adds them to the HashMap. So it will loop through all of the pages in the pagination list until it is completed. It will go back to the Page 1 which is saved as Constants.url.

    It gets to page 5 but then gets stuck, I am not sure how to code in the other examples of 6, 7, 8 and clicking the Next button each time to access them, adding them to pagination list.

    Note: The Thread.sleep methods are there to enable the page time to load all elements on the page.

    Any ideas?