简体   繁体   中英

How to fix selenium.common.exceptions.NoSuchElementException?

I am trying to find when the seller became a seller on bol.com by doing

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:

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:

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM