python - Python / Selenium - 如何为 html 获取正确的 xpath
问题描述
从几天开始,我试图找出网站上按钮的正确 XPATH 是什么。
这是按钮的html代码:
<div class="rc_library_element_name rc_actionable ia-inline-block" href="reporteditor.phtml?.op=3277&.cr=._%21Mqxtjfi_SFjudmo_SWfssuv&.sess=8Otpr5-9Bz_wpGJTXqAEAPTCP7GkYg..&.done=WvKqgMCoA3IAAEf5xL0AAAAK8">Invoice Detail Report</div>
我尝试了几种方法,例如:
invoice_detail_report = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/div[2]/div[2]/div[2]/div/div[2]/div[5]/div[2]/div[17]/table/tbody/tr/td[2]/div[1]')))
或者
invoice_detail_report = wait.until(EC.element_to_be_clickable((By.XPATH, '//DIV[@class="rc_library_element_name rc_actionable ia-inline-block"][text()="Invoice Detail Report"]')))
不幸的是,这些都没有奏效。
您能否告知该按钮的正确 xpath 是什么?
非常感谢您的帮助,
问候。
解决方案
要将带有文本的元素定位为Invoice Detail Report并在其上调用click()
,您可以使用以下任一策略:
将XPATH 与文本一起使用:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(.,'Invoice Detail Report')]"))).click()
将XPATH 与属性一起使用:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='c_library_element_name rc_actionable ia-inline-block' and contains(@href,'reporteditor.phtml')]"))).click()
XPATH 使用所有属性:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='c_library_element_name rc_actionable ia-inline-block' and contains(@href,'reporteditor.phtml') and contains(.,'Invoice Detail Report')]"))).click()
推荐阅读
- r - 错误:对于 SPSS 到 R 的转换,`labels` 必须是唯一的
- python - “超级”对象没有属性“对象”Python Django
- ios - 可以在部分视图上允许HitTesting 吗?
- javascript - 当我使用 event.keycode 执行我的功能时,它不会执行,但如果发出警报,它会起作用
- linux - 如何确定服务器安装是否远程完成
- css - 为什么 display: inline-block 给这个 h1 元素一个填充?
- tcl - 如何通过单击过程的调用导航到记事本++中的tcl proc主体
- c# - 我无法在 asp.net 核心中创建迁移,因为我一直无法构造数据库上下文并且我使用 2 个上下文
- r - R 不正确地计算大于或等于随机数据
- r - 为什么在尝试使用 rstanarm 包部署应用程序时,Shiny 会出错