我正在使用Python 3.6+和Selenium 3.141
我正在尝试从页面获取元素,尽管我使用的是正确的Xpath表达式(在浏览器控制台中已确认),但相同的Xpath表达式在Selenium chrome驱动程序中引发了“ NotFound”错误。
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表达式不能与硒一起使用?我该如何解决?
页面源中缺少必需的内容-它是动态加载的,因此您需要等待直到它出现在DOM中:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://live.euronext.com/en/product/stock-options/AH1-DPAR'
browser = webdriver.Chrome(executable_path='./chromedriver')
browser.get(url)
checkbox = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="form-options-index"]/div/div[2]')))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句