[英]Python selenium find_element_by_xpath not finding existing element in page
我正在使用 Python 3.6+ 和 Selenium 3.141
我试图从页面中获取一个元素,尽管我使用了正确的 Xpath 表达式(在浏览器控制台中确认),但相同的 Xpath 表达式在 Z8E00596AD8DE2213FF8ZDchrome 驱动程序中引发了“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 表达式不适用于 selenium? 我该如何解决?
点击Select all
复选框。 使用WebDriverWait
() 并等待element_to_be_clickable
() 并遵循Xpath
。
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="form-options-index"]//label[@for="select_all_dates"]'))).click()
您需要导入以下库。
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
页面源中缺少必需的内容 - 它是动态加载的,因此您需要等到它出现在 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]')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.