如何修复带有超时异常错误的硒代码?

时间:2019-09-18 21:34:20

标签: python selenium

我正在尝试使用python创建一个硒脚本,该脚本显示bol.com上卖家的评论数量。每次,我都会收到错误selenium.common.exceptions.TimeoutException: Message:。这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://www.bol.com/nl/v/looliving-nl/1146429/")
reviews = WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,"//div[@class='media_body'/following::p"))).text
print(reviews)

我该如何修复我的脚本?

2 个答案:

答案 0 :(得分:0)

您是否尝试过requestslxml?这可能会解决您的问题...但是就像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") 

答案 1 :(得分:0)

不正确的表达式://div[@class='media_body'/following::p

您可以使用以下xpath//div[@class='media__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