【发布时间】:2019-04-29 02:21:47
【问题描述】:
不断收到unable to locate element 错误消息。
xpath 的第一个查找元素很好,但第二个让我很难。
这是我的代码:
import XLUtils
from selenium import webdriver
driver=webdriver.Chrome(executable_path="C:\Chrome_Driver\Chromedriver.exe")
driver.get("https://www.canada.ca/en/revenue-agency/services/e-services/e-services-businesses/payroll-deductions-online-calculator.html")
driver.find_element_by_xpath('/html/body/main/div[1]/div[7]/p/a[1]').click()
driver.find_element_by_xpath('//*[@id="welcome_button_next"]').click()
【问题讨论】:
-
您不想使用具有那么多级别或索引的 XPath,因为这会使它们变得脆弱(更容易损坏)。更好的第一个定位器是 CSS 选择器,
a.btn-primary。对于第二个定位器,当您只有一个 ID 时,不要通过 XPath 查找。而是使用.find_element_by_id("welcome_button_next"). -
另外,您需要将相关的 HTML 添加到问题中。提供链接很好,但如果 HTML 将来发生变化,这个问题将没有用,也不能独立存在。
标签: python selenium xpath css-selectors webdriverwait