Trying to select the "Follow" buttons from a Instagram user followers list like this one:
I'm using this function to get the buttons. That weird string is the class that instagram uses for the buttons, and ChromeUtils.findByXpaths() is just a shortcut to driver.findElements(By.xpath(xpath));
private static List<WebElement> getFButtons(){
return ChromeUtils.findByXpaths("//button[@class=\"sqdOP L3NKy y3zKF \"]");
}
It works fine for on the first user but when it clicks one of the last one, Instagram loads more users on the list, and then Selenium starts missing a lot of "Seguir" buttons, even if they have exactly the same class, sqdOP L3NKy y3zKF
Example of missed buttons and their class
Then if scrolled manually to load more users, it starts working again only with some buttons, it looks like that it follows a random pattern. I also tried selecting the buttons by its text, "Seguir" but no success. This is the code that interacts with the buttons:
int i = 0;
int max = r.getMaxFollowPSession();
List<WebElement> fButtons = getFButtons();
while (i < max) {
try {
// This if checks if it selected the follow button from the main page, I added a sout to check if
// this was the problem but it isn't
if (fButtons.get(i).getAttribute("class").equals(CLASS_BIG_FOLLOW_BUTTON)) {
i++;
max++;
System.out.println("Mistake with follow page button, selectiong next button");
continue;
}
fButtons.get(i).click();
Thread.sleep(1000);
i++;
} catch (IndexOutOfBoundsException e){
fButtons = getFButtons(); // Once the list of selected buttons end, it selects the new ones.
}
}
Am I missing something or is this a Selenium bug? Thank you in advance