微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Selenium 错过了 Instagram 用户关注者列表中“关注”按钮上的随机元素

如何解决Selenium 错过了 Instagram 用户关注者列表中“关注”按钮上的随机元素

尝试从 Instagram 用户关注者列表中选择“关注”按钮,如下所示:

Instagram followers list

我正在使用此功能获取按钮。那个奇怪的字符串是 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?