首页 > 解决方案 > 如何通过 xpath 获取此 html 代码以在 python 上使用 selenium?

问题描述

我有一个更新按钮,在 python 中,机器人我想点击这个按钮,但我尝试了很多选项但不起作用我无法通过 XPath 定位元素。请帮我?谢谢你的帮助。

注意:我需要使用文本“更新”来定位,因为网页上有很多按钮。

<span class="a-spacing-top-small">
  <span class="a-button a-button-primary a-button-small sc-update-link" id="a-autoid-2">
    <span class="a-button-inner">
      <a href="javascript:void(0);" data-action="update" class="a-button-text" role="button" id="a-autoid-2-announce" style="">Update
        <span class="aok-offscreen">Nike Academy 18 Rain Jacket Men's (Obsidian, M)</span>
      </a>
    </span>
  </span>
</span>

标签: pythonseleniumxpathautomated-tests

解决方案


这应该有效:

button = driver.find_element_by_xpath("//a[@data-action,'update']")

其中驱动程序是您的驱动程序实例。


推荐阅读