【问题标题】:How do I find the unfollow button on Instagram using Selenium and Python?如何使用 Selenium 和 Python 在 Instagram 上找到取消关注按钮?
【发布时间】:2021-02-23 18:15:49
【问题描述】:

我想使用 selenium 在下图中找到 XPath 按钮:

但是因为顶部按钮上的XPath按钮每次都会变化,所以我决定为它做一个XPath:

driver.find_element_by_xpath('//button[@text=""]').click()

运行时,我的程序报错xpath无效

我很好地研究了 xpath 构建方法,但找不到适合我的程序的解决方案。

如果您有解决方案,请写在答案部分

非常感谢

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    这是一个要使用的 xpath:

    //button[.//span[@aria-label='Following']]
    

    它选择具有 aria-label 属性的子 span 节点的任何按钮。这适用于 intagram,因为 dom 看起来像:

    <span class="vBF20 _1OSdk">
      <button class="_5f5mN    -fzfL     _6VtSN     yZn4P   ">
        <div class="                     Igw0E   rBNOH          YBx95       _4EzTm                                                                                                              " style="height: 28px;">
          <span aria-label="Following" class="glyphsSpriteFriend_Follow u-__7">
          </span>
        </div>
      </button>
    </span>
    

    【讨论】:

      【解决方案2】:

      [@text=""] 谓词“假定”您要选择具有 text 属性和空值的元素 (&lt;button text=""&gt;button text&lt;/button&gt;)。

      如果你想选择带有 空文本节点的元素 (&lt;button&gt;&lt;/button&gt;) 试试

      '//button[text()=""]'
      

      【讨论】:

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