【问题标题】:Can't locate / interact with button on Instargram via. selenium无法通过 Instagram 定位/与按钮交互。硒
【发布时间】:2022-01-13 20:09:21
【问题描述】:

我正在尝试使用 selenium 创建一个简单的 python 程序,该程序需要找到并单击关注按钮。

这是 HTML 代码:

<div class="             qF0y9          Igw0E   rBNOH          YBx95   ybXk5    _4EzTm                      soMvl                                                                                        " id="f2d16737c928a4">

<button class="sqdOP  L3NKy   y3zKF     " type="button">Abonnieren</button>
</div>

我的python代码:

example = driver.find_element_by_xpath("//button[contains(@class,'sqdOP  L3NKy   y3zKF     ')]")
example.click

每次我尝试它都会给我:“selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element”

如果您想查看该网站,您需要登录并转到 Instagram 建议您应该关注的人的建议页面:https://www.instagram.com/explore/people/

【问题讨论】:

  • 这个按钮是你试图点击关注按钮吗?
  • @Luke Hamilton 是的
  • 让我知道您对我的回答的看法!乐于助人。

标签: python python-3.x selenium instagram


【解决方案1】:
driver.find_element_by_xpath("//button[.='Abonnieren']").click()

只需查找带有该文本的按钮并单击它。

【讨论】:

    【解决方案2】:

    您共享的 XPATH 似乎是一个随机字符串,不会轻易地在其他关注按钮中迭代。我建议您通过 HTML 页面获取“完整的 XPATH”,这样您就可以进行循环操作。点击关注按钮的循环见以下代码:

    count=1
    while True:
          try:
                driver.find_element_by_xpath("/html/body/div[1]/div/div/section/main/div/div[2]/div/div/div["+str(count)+")]/div[3]/button").click()
                count=count+1
          except:
                break
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      相关资源
      最近更新 更多