我正在尝试通过以下方式找到卖家在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)
我希望它能打印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"}
我该如何解决?
答案 0 :(得分:1)
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
或只是简单地将隐式等待
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)
答案 1 :(得分:0)
使用WebDriverWait等待元素出现:
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"))
)
答案 2 :(得分:0)
要打印日期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)