Python 如何修复带有超时异常错误的selenium代码?
我试图用python编写一个selenium脚本,显示一个卖家在bol.com上有多少评论。每次,我都会收到错误Python 如何修复带有超时异常错误的selenium代码?,python,selenium,Python,Selenium,我试图用python编写一个selenium脚本,显示一个卖家在bol.com上有多少评论。每次,我都会收到错误selenium.common.exceptions.TimeoutException:Message:。这是我的密码: 从selenium导入webdriver 从selenium.webdriver.common.by导入 从selenium.webdriver.support.ui导入WebDriverWait 从selenium.webdriver.support将预期的_条件
selenium.common.exceptions.TimeoutException:Message:
。这是我的密码:
从selenium导入webdriver
从selenium.webdriver.common.by导入
从selenium.webdriver.support.ui导入WebDriverWait
从selenium.webdriver.support将预期的_条件导入为EC
driver=webdriver.Firefox()
驱动程序。获取(“https://www.bol.com/nl/v/looliving-nl/1146429/")
reviews=WebDriverWait(driver,30).until(位于((By.XPATH,“//div[@class='media\u body'/following::p”))的元素的EC.visibility
印刷(评论)
如何修复我的脚本?您是否尝试过
请求
和lxml
?这可能会解决您的问题……但就像Greg提到的,这取决于网站来源
import requests
from lxml import html
page = requests.get("https://www.bol.com/nl/v/looliving-nl/1146429/")
tree = html.fromstring(page.content)
reviews = tree.xpath("//div[@class='media_body'/following::p")
表达式不正确:
//div[@class='media\u body'/following::p
您可以使用这个xpath
://div[@class='media\uu body']//p
reviews = WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,"//div[@class='media__body']//p"))).text
print(reviews)
控制台输出:
Totaal aantal beoordelingen: 6
在页面上看不到任何HTML的情况下,我敢打赌有内容是使用JavaScript异步加载的。在加载这些元素之前,您可能需要以另一种方式滚动或与页面交互。我正在查看该页面,但该元素不在那里。