Python 3.x Python-Selenium XPATH查询不匹配
我已经使用Chrome SelAssist扩展测试了一个XPATH查询,它运行得非常好。 语法为“/*[@id=“fbPhotoSnowliftTimestamp”]/a/abbr: 我已经开始编写python代码来检测这样的元素:Python 3.x Python-Selenium XPATH查询不匹配,python-3.x,selenium,selenium-chromedriver,Python 3.x,Selenium,Selenium Chromedriver,我已经使用Chrome SelAssist扩展测试了一个XPATH查询,它运行得非常好。 语法为“/*[@id=“fbPhotoSnowliftTimestamp”]/a/abbr: 我已经开始编写python代码来检测这样的元素: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys chrome_
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
chrome_profile = r"C:\Users\XXX\AppData\Local\Google\Chrome\User Data"
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=' + chrome_profile)
w = webdriver.Chrome(executable_path="C:\\Projects\\selenium\\chromedriver.exe", chrome_options=options)
w.get('https://website.com')
test = w.find_element(By.XPATH, "//*[@id=\"fbPhotoSnowliftTimestamp\"]/a/abbr")
页面加载良好,和我手动测试的页面完全相同。
很遗憾,我继续检索此错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="fbPhotoSnowliftTimestamp"]/a/abbr"}
(Session info: chrome=80.0.3987.149)
我不知道我做错了什么。
谢谢您的建议。页面加载速度可能没有预期的快。如果使用下面的方法找不到xpath,将引发超时异常。尝试:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_profile = r"C:\Users\XXX\AppData\Local\Google\Chrome\User Data"
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=' + chrome_profile)
w = webdriver.Chrome(executable_path="C:\\Projects\\selenium\\chromedriver.exe", chrome_options=options)
w.get('https://website.com')
test = WebDriverWait(w, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[@id=\"fbPhotoSnowliftTimestamp\"]/a/abbr")))
#w.find_element(By.XPATH, "//*[@id=\"fbPhotoSnowliftTimestamp\"]/a/abbr")