Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python selenium通过xpath查找元素在页面中找不到现有元素_Python_Selenium_Xpath - Fatal编程技术网

Python selenium通过xpath查找元素在页面中找不到现有元素

Python selenium通过xpath查找元素在页面中找不到现有元素,python,selenium,xpath,Python,Selenium,Xpath,我正在使用Python 3.6+和Selenium 3.141 我试图从页面获取元素,尽管我使用的是正确的Xpath表达式(在浏览器控制台中确认),但相同的Xpath表达式在selenium chrome驱动程序中会引发“NotFound”错误 myscript.py 脚本在调用find_element\u by_xpath()方法时引发异常-即使在使用浏览器时,相同的xpath表达式也会导致正确识别/选择元素 为什么Xpath表达式不能与selenium一起使用?如何修复此问题?页面源中缺少必

我正在使用Python 3.6+和Selenium 3.141

我试图从页面获取元素,尽管我使用的是正确的Xpath表达式(在浏览器控制台中确认),但相同的Xpath表达式在selenium chrome驱动程序中会引发“NotFound”错误

myscript.py 脚本在调用
find_element\u by_xpath()
方法时引发异常-即使在使用浏览器时,相同的xpath表达式也会导致正确识别/选择元素


为什么Xpath表达式不能与selenium一起使用?如何修复此问题?

页面源中缺少必需的内容-它是动态加载的,因此您需要等待它出现在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]')))

要单击
选择全部
复选框。使用
WebDriverWait
(),等待
元素可点击
()并遵循
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
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait