尝试从 Instagram 用户关注者列表中选择“关注”按钮,如下所示:
我正在使用此功能来获取按钮。那个奇怪的字符串是 instagram 用于按钮的类,ChromeUtils.findByXpaths()
只是一个快捷方式driver.findElements(By.xpath(xpath));
private static List<WebElement> getFButtons(){
return ChromeUtils.findByXpaths("//button[@class=\"sqdOP L3NKy y3zKF \"]");
}
它适用于第一个用户,但是当它单击最后一个用户时,Instagram 会在列表中加载更多用户,然后 Selenium 开始丢失很多“Seguir”按钮,即使它们具有完全相同的类,sqdOP L3NKy y3zKF
然后,如果手动滚动以加载更多用户,它只会使用一些按钮重新开始工作,看起来它遵循随机模式。我还尝试通过文本“Seguir”选择按钮,但没有成功。这是与按钮交互的代码:
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.
}
}
我错过了什么还是这是一个 Selenium 错误?先感谢您