如何解决Selenium 错过了 Instagram 用户关注者列表中“关注”按钮上的随机元素
尝试从 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
Example of missed buttons and their class
然后如果手动滚动以加载更多用户,它只会通过一些按钮重新开始工作,看起来它遵循随机模式。我还尝试通过文本“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 错误? 提前致谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。