【发布时间】:2021-03-08 11:29:09
【问题描述】:
我正在使用 Python 3.6+ 和 Selenium 3.141
我正在尝试从页面中获取元素,虽然我使用了正确的 Xpath 表达式(在浏览器控制台中确认),但相同的 Xpath 表达式在 selenium chrome 驱动程序中引发了“NotFound”错误。
myscript.py
>from selenium import webdriver
url = 'https://live.euronext.com/en/product/stock-options/AH1-DPAR'
browser = webdriver.Chrome(executable_path='./chromedriver')
browser.get(url)
try:
checkbox = browser.find_element_by_xpath('//*[@id="form-options-index"]/div/div[2]')
except:
pass
脚本会在调用 find_element_by_xpath() 方法时引发异常 - 即使在使用浏览器时,相同的 Xpath 表达式也会导致正确识别/选择元素。
为什么 Xpath 表达式不适用于 selenium?我该如何解决这个问题?
【问题讨论】: