简体   繁体   English

当我使用 python selenium headless 时出现 tImeout 错误

[英]Getting tImeout error when I am using python selenium headless

Without using the selenium headless the following code works fine.如果不使用无头硒,以下代码可以正常工作。 But Why I am getting error when I am using headless mode.但是为什么我在使用无头模式时会出错。 Here is my code:-这是我的代码:-

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import time


options = Options()
options.add_argument("--disable-notifications")
options.headless = True
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)

url = "https://www.justdial.com/Delhi/S-K-Premium-Par-Hari-Nagar/011PXX11-XX11-131128122154-B8G6_BZDET"
driver.get(url)


pop_up = WebDriverWait(driver, 30).until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="best_deal_detail_div"]/section/span')))
time.sleep(2.5)
pop_up.click()  # For disable pop-up



while True:
     try:
       element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, "//span[text()='Load More Reviews..']")))
       element.click()

    except TimeoutException:
       break
    except:
       pass


print([span.text for span in driver.find_elements_by_css_selector('span.rName.lng_commn')])

Kindly give some suggestions or solution.请提供一些建议或解决方案。 Thank you.谢谢你。

The root cause of the error is clear.错误的根本原因是明确的。 It's happening as a result of the script not being able to locate the element -that is held by an explicit-wait .这是由于脚本无法定位元素 - 即由explicit-wait持有的元素而发生explicit-wait

Given that you tried the answers mentioned in the comments then this could also be to do with the anomaly behaviour issue in non-headless and headless mode.鉴于您尝试了评论中提到的答案,那么这也可能与non-headlessheadless模式下的异常行为问题有关。 There are situations in which a headless mode cannot detect elements by their id in which case pass in the full absolute-path of the DOM .在某些情况下,无头模式无法通过id检测元素,在这种情况下,传入DOM的完整absolute-path

Eg In the following, pass full xpath and see if it solves the problem.例如,在下面,传递full xpath并查看它是否解决了问题。

element = WebDriverWait(driver, 20).until(
        EC.element_to_be_clickable((By.XPATH, <full xpath>)))
       element.click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有 python selenium 的无头 chrome 时出现“403 Forbidden”错误 - Getting '403 Forbidden' error when using headless chrome with python selenium 使用带有 Python 的 Selenium Webdriver 在 Headless chrome 浏览器上运行脚本时发生超时错误 - Timeout Error occurred When run a script on Headless chrome browser by using Selenium Webdriver with Python 即使在 selenium python 中使用 try catch 后,我也会遇到超时异常 - I am getting timeout exception even after using try catch in selenium python 我在 selenium python 上收到此错误 - I am getting this error on selenium python 使用 Firefox 无头、Selenium 和 Python 时出错 - Error while using Firefox headless, Selenium and Python 在无头模式下使用 Selenium 时出现 403 错误? - 403 Error when using Selenium in headless mode? Python,PhantomJS 说我没有使用无头? - Python, PhantomJS says I am not using headless? 嗨,当使用 python 和 selenium 时,我无法通过 cloudflare 访问使用 DDos 保护的站点 - Hi, I am having problems with getting through to a site using DDos protection by cloudflare, when using python and selenium 从命令行运行python硒脚本时出现固定错误 - I am getting fixture error when running python selenium scripts from command line Python Selenium:我正在获取元素未附加到页面文档错误 - Python Selenium: I am getting element is not attached to the page document error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM