自动关注 Instagram 用户时遇到问题

时间:2021-01-23 17:06:19

标签: python selenium selenium-webdriver automation instagram

我的代码转到特定页面,然后打开最后一个帖子并尝试关注“喜欢者”。现在的问题是有 3 种类型的按钮:“关注”、“关注”和“请求”。我的程序只能点击“关注”按钮,如果它到达带有“关注”或“询问”的按钮,它就会崩溃。我希望它跳过这两种类型的按钮。

在此 screenshot 中,程序开始跟踪前 4 个用户,然后在第 5 个用户处崩溃。

这是我写的代码:

def follow_likers_v4(self, root_name, number_follower):
    # following = []
    time.sleep(3)

    self.browser.get(f"https://www.instagram.com/{root_name}/")
    time.sleep(3)

    first_post = self.wait_for_objects(By.CSS_SELECTOR, ".eLAPa")
    first_post[0].click()
    time.sleep(3)

    get_likers = self.wait_for_objects(By.XPATH, '//div[contains(@class,"Nm9Fw")]/button')
    get_likers[0].click()
    time.sleep(3)

    for i in range(1, 5):
        src1 = self.wait_for_object(
            By.XPATH, f"/html/body/div[6]/div/div/div[2]/div/div/div[{i}]/div[2]/div/div/span/a"
        )
        self.browser.execute_script("arguments[0].scrollIntoView();", src1)
        time.sleep(1)

    for follow in range(1, number_follower + 1):
        follow_button = self.wait_for_objects(
            By.XPATH, f'/html/body/div[6]/div/div/div[2]/div/div/div[{follow}]/div[3]/button[text()="Follow"]'
        )
        follow_button[0].click()
        time.sleep(1)

0 个答案:

没有答案