'How to store List of Elements which are present in multiple Page?

There is a button called "Responses" total number of button is 100 which are store in 2 different webpages, from 1st page to 2nd page navigation button called "Next"

I need to store all the button in a list and verify all 100 text is the same as "Responses" button is present. But I don't know how to continue loop through next page and store total 100 button.

By nextBtn = By.xpath("//input[@type='submit' and @value='Next']");

List<WebElement> allresponses= diver.findElements(By.xpath("//input[@type='button' and @value='Responses']"));
List<String> responseText = new ArrayList<>();
for(int i=0; i<allresponses.size(); i++){
responseText.add(allresponses).getText();}

I'm trying to store all 100 button from 2 pages to 1 List and verify there is 100btns. 1page 50btn and both page after clickn next will be another 50btn. Need help as I'm stuck in the middle.



Solution 1:[1]

you want tot loop through allresponses and add each element's text, not the element itself:

By nextBtn = By.xpath("//input[@type='submit' and @value='Next']");

List<WebElement> allresponses= diver.findElements(By.xpath("//input[@type='button' and @value='Responses']"));
List<String> responseText = new ArrayList<>();
for(int i=0; i<allresponses.size(); i++){
    responseText.add(allresponses[i].getText());
}

Solution 2:[2]

This might help

By nextBtn = By.xpath("//input[@type='submit' and @value='Next']");
List < WebElement > allresponses = new List < WebElement > ();
while (diver.findElements(By.xpath("//input[@type='button' and @value='Responses']")).isEnabled()) {

    allresponses.addAll(diver.findElements(By.xpath("//input[@type='button' and @value='Responses']")));

}

Solution 3:[3]

Here, what you can do is store the elements of Page one in one list. And elements from other page in second list. And after that you can add element of these lists.
Please check the following code:

By nextBtn = By.xpath("//input[@type='submit' and @value='Next']");
List<WebElement> allresponses= driver.findElements(By.xpath("//input[@type='button' and @value='Responses']"));
List<String> responseText = new ArrayList<>();
    for(int i=0; i<allresponses.size(); i++)
    { 
    responseText.add(allresponses[i].getText());
    }
    driver.findElement(nextBtn).click();
List<WebElement> allresponses1= driver.findElements(By.xpath("//input[@type='button' and @value='Responses']"));
List<String> responseText1 = new ArrayList<>();
    for(int i=0; i<allresponses1.size(); i++)
    { 
    responseText1.add(allresponses1[i].getText());
    }
responseText.addAll(responseText1);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DMart
Solution 2 Rahul Das
Solution 3