简体   繁体   English

如何修复selenium.common.exceptions.NoSuchElementException?

[英]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你需要诱导WebDriverWaitvisibility_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.

相关问题 如何修复“selenium.common.exceptions.NoSuchElementException” - How can I fix "selenium.common.exceptions.NoSuchElementException" selenium.common.exceptions.NoSuchElementException - selenium.common.exceptions.NoSuchElementException Selenium 中的 selenium.common.exceptions.NoSuchElementException - selenium.common.exceptions.NoSuchElementException in Selenium selenium.common.exceptions.NoSuchElementException:消息: - selenium.common.exceptions.NoSuchElementException: Message: “错误:selenium.common.exceptions.NoSuchElementException:消息 - "Error: selenium.common.exceptions.NoSuchElementException: Message Selenium 使用 Chrome 时出现“selenium.common.exceptions.NoSuchElementException” - Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome Selenium.common.exceptions.NoSuchElementException 错误,即使显式等待 - Selenium.common.exceptions.NoSuchElementException error even with explicit waiting 动态内容中的selenium.common.exceptions.NoSuchElementException - selenium.common.exceptions.NoSuchElementException from dynamic content selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素 - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素: - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM