【问题标题】:Selenium Clicking all follow buttons on instagram PythonSelenium 单击instagram Python上的所有关注按钮
【发布时间】:2020-04-04 06:16:35
【问题描述】:

当您在 Instagram 网站上点击用户的用户名时,我正在尝试自动点击 Instagram 关注按钮。

点击用户名后,点击关注者,会打开一个窗口,其中包含关注此人的人,并且有关注按钮

这是新窗口的截图

我正在尝试通过 python selenium 一个一个地单击按钮,但我尝试的任何操作似乎都不起作用。

我得到的最好的结果是一个 for 循环,它使用 xpath 只点击了第一个跟随按钮,但没有点击其他按钮。

#click the followers button to dispaly the users followers
driver.find_element_by_partial_link_text("followers").click()
time.sleep(3)
#scroll through the followers list to a specified heeight
scroll_box=driver.find_element_by_xpath("/html/body/div[4]/div/div[2]")
last_ht, ht=0, 1
while last_ht !=ht:
    last_ht=ht
    time.sleep(2)
    ht=driver.execute_script("""arguments[0].scrollTo(0, 2000);
    return 2000;
    """, scroll_box)
#follow users up to the specified height
follow=driver.find_elements_by_xpath("/html/body/div[4]/div/div[2]/ul/div/li[1]/div/div[3]/button")
for x in range (0,len(follow)):
        follow[x].click()
        time.sleep(2)

    time.sleep(1)

【问题讨论】:

    标签: python selenium automation instagram


    【解决方案1】:

    您的 Xpath 选择器似乎是直接从 chrome 开发人员工具复制而来的,顺便说一句,它只会返回一个按钮,因为您的目标是一个 li

    # Get all buttons that has the text Follow 
    buttons = driver.find_elements_by_xpath("//button[contains(.,'Follow')]")
    for btn in buttons:
        # Use the Java script to click on follow because after the scroll down the buttons will be un clickeable unless you go to it's location
        driver.execute_script("arguments[0].click();", btn)
        time.sleep(2)
    

    【讨论】:

    • 非常感谢。这正是我所需要的。非常感谢
    【解决方案2】:

    如果我们已经关注了某人并且如果它问我这个怎么办:

    如果我想在这段代码中添加if 语句:

    #scroll through the followers list to a specified heeight
    # Get all buttons that has the text Follow 
    buttons = self.driver.find_elements_by_xpath("//button[contains(.,'Follow')]")
    
    for btn in buttons:
        self.driver.execute_script("arguments[0].click();", btn)
        sleep(99)
    

    后来现场开始发这个消息it is blocking my action to click on followe

    【讨论】:

      【解决方案3】:

      Yash,我无法评论你的回答,所以我再发一个。

      解决方案: 点击前需要检查按钮文字,

      if btn.text == 'Follow':
          #Code to click button
      

      【讨论】:

        猜你喜欢
        • 2020-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多