python - 使用 id、xpath、class 等单击按钮时遇到问题

标签 python selenium button xpath webdriver

我正在尝试单击一个按钮,并且需要小组的一些帮助。这个按钮位于我的 WordPress 网站的后端/管理区域(我正在尝试将数据批量上传到目录列表系统中)。我认为我需要使用按钮类,因为同一页面上有两个具有相同编号的 data-id 元素。我已经提供了我的 Selenium 代码(python)尝试以及我尝试访问的html。任何帮助表示赞赏!

HTML:

<div class="pkg-button">
    <a data-id="38579" class="btn btn-lg btn-primary button select-plan">Select</a>
</div>

这是具有冲突 ID 的 html 代码片段。

<ul data-price="0"  data-subscribed='0' data-id="38579" data-type="1"  class="packagelistitems " >

============

代码方法1:

elem = driver.find_element_by_id("38579").click()

代码方法2:

driver.find_element_by_class_name('btn btn-lg btn-primary button select-plan').click()

代码方法3:

elements = driver.find_elements_by_class_name("btn btn-lg btn-primary button select-plan")
for e in elements:
    e.click()

代码方法4:

driver.find_element_by_xpath('//*[@id="plan"]/div[1]/ul/li/div/div/div[2]/div[2]/a').click()

对于最后一个代码片段 (#4),我收到以下错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a data-id="38579" class="btn btn-lg btn-primary button select-plan">...</a> is not clickable at point (659, 14). Other element would receive the click: <div id="wpadminbar" class="">...</div>
  (Session info: chrome=61.0.3163.100)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64)

最佳答案

尚不清楚为什么元素不可点击(可能您应该点击 div 包装器而不是 a 元素),所以尝试这些,应该可以工作)

点击div:

//div[@class='pkg-button'][a[text()='Select']]

点击a标签

//div[@class='pkg-button']/a[text()='Select']

//a[text()='Select']

重要提示:请注意,在方法 4 中,您收到了错误,其中 id 为 wpadminbar 的元素与您的项目重叠,因此请在浏览器开发工具中检查这一点,以确保某些 div 是不与您要单击的元素重叠 =)

关于python - 使用 id、xpath、class 等单击按钮时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46851646/

相关文章:

jquery - Nifty Modal - 如何在没有按钮的情况下触发模式

php - PHP 事件监听器中的多个回显删除编辑按钮

python - XBMC 12.0 (Frodo) 使用哪个版本的 Python?

python - Pandas 修剪数据的更好方法

python - Ubuntu Python 3 上的 Pygame

python - 使用 ABC 和 ABCMeta 有什么区别吗?

flash - 如何管理主时间线上的动画片段按钮控件声音?

c# - Selenium C# - 查找复杂网页中 IFrame 内的所有链接

python - 使用 Selenium 和 BeautifulSoup 提取 iFrame 内容

java - 如何在网页的某个框架(以div分隔)上向下滚动?