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

时间:2021-03-25 20:57:21

标签: java selenium selenium-webdriver instagram selenium3

尝试从 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 错误? 提前致谢

0 个答案:

没有答案