[英]How to fix selenium.common.exceptions.NoSuchElementException?
I am trying to find when the seller became a seller on bol.com
by doing 我试图通过这样做来查找卖家何时成为
bol.com
上的卖家
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.bol.com/nl/v/looliving-nl/1146429/")
date = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/p").text
print(date)
I expected it to print Actief sinds: 29 januari 2016
, but instead it returns an error about my find_element_by_xpath()
query: 我希望它可以打印
Actief sinds: 29 januari 2016
,但是它返回有关我的find_element_by_xpath()
查询的错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/p"}
How would I fix this? 我该如何解决?
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://www.bol.com/nl/v/looliving-nl/1146429/")
date = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/p")).text
OR simply put implicit wait 或简单地将隐式等待
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.bol.com/nl/v/looliving-nl/1146429/")
driver.implicitly_wait(10)
date = driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/p").text
print(date)
Use a WebDriverWait to wait for the elment to be present: 使用WebDriverWait等待元素出现:
https://selenium-python.readthedocs.io/waits.html https://selenium-python.readthedocs.io/waits.html
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPath, "/html/body/div[1]/div[2]/div/div[1]/div/div[1]/div/p"))
)
To print the date Actief sinds: 29 januari 2016
You need to Induce WebDriverWait
and visibility_of_element_located
and following xpath. 打印日期
Actief sinds: 29 januari 2016
你需要诱导WebDriverWait
和visibility_of_element_located
和下面的XPath。
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 import webdriver
driver=webdriver.Firefox()
driver.get("https://www.bol.com/nl/v/looliving-nl/1146429/")
print(WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,"//h1[@data-test='seller_name']/following::p[@data-test='seller_active']"))).text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.