当前位置: 首页 > 知识库问答 >
问题:

Selenium在一些执行时间后为所有网站提供“从渲染器接收消息超时”

丰智
2023-03-14

我有一个应用程序,我需要一个长时间运行的Selenium web驱动程序实例(我在无头模式下使用Chrome驱动程序83.0.4103.39)。基本上,应用程序不断地从队列中提取url数据,并将提取的url提供给Selenium,Selenium应该对网站进行一些分析。许多这样的网站可能会关闭,无法访问或中断,所以我设置了10秒的页面加载超时,以避免Selenium永远等待页面加载。
我这里遇到的问题是,在一些执行时间之后(比方说10分钟),Selenium开始为每个URL给出超时接收来自呈现器的消息错误。最初它工作正常,它正确地打开好的网站,并在坏的网站上超时(网站无法加载),但过了一段时间后,它开始给一切超时,即使是应该正确打开的网站(我已经检查过了,它们在Chrome浏览器上正确打开)。我很难调试这个问题,因为应用程序中的每个异常都被正确捕获。我还注意到,这个问题只在headless模式下发生。

  • 更新*
    在网站分析过程中,我还需要考虑iframe(只有顶层),因此我还添加了一个逻辑来将驱动程序上下文切换到主页面中的每个iframe并提取相关的HTML。
import traceback
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

width = 1024
height = 768

chrome_options = Options()
chrome_options.page_load_strategy = 'normal'
chrome_options.add_argument('--enable-automation')
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--lang=en')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--allow-insecure-localhost')
chrome_options.add_argument('--allow-running-insecure-content')
chrome_options.add_argument('--disable-notifications')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-browser-side-navigation')
chrome_options.add_argument('--mute-audio')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--force-device-scale-factor=1')
chrome_options.add_argument(f'window-size={width}x{height}')

chrome_options.add_experimental_option(
    'prefs', {
        'intl.accept_languages': 'en,en_US',
        'download.prompt_for_download': False,
        'download.default_directory': '/dev/null',
        'automatic_downloads': 2,
        'download_restrictions': 3,
        'notifications': 2,
        'media_stream': 2,
        'media_stream_mic': 2,
        'media_stream_camera': 2,
        'durable_storage': 2,
    }
)

driver = webdriver.Chrome(options=options)
driver.set_page_load_timeout(10)  # Timeout 10 seconds

# Polling queue
while True:
    url = queue.pop()

    # Try open url
    try:
        driver.get(url)
    except BaseException as e:
        print(e)
        print(traceback.format_exc())
        continue

    # Take website screenshot
    png = driver.get_screenshot_as_png()

    # Extract html from iframes (if any)
    htmls = [driver.page_source]
    iframes = driver.find_elements_by_xpath("//iframe")

    for index, iframe in enumerate(iframes):
        try:
            driver.switch_to.frame(index)
            htmls.append(driver.page_source)
            driver.switch_to.default_content()
        except BaseException as e:
            print(e)
            print(traceback.format_exc())
            continue

    # Do some analysis
    for html in htmls:
        # ...
        pass

    # Wait a bit
    sleep(0.1)
Opening https://www.yourmechanic.com/user/appointment/3732777/?access_token=HLZYIg&ukey=6quWpg1724633&rcode=abttgi&utm_medium=sms&utm_source= rb
LOAD EXCEPTION Message: timeout: Timed out receiving message from renderer: 10.000
  (Session info: headless chrome=83.0.4103.116)

Traceback (most recent call last):
  File "/Users/macbmacbookpro4ookpro4/Documents/Projects/python/proj001/main.py", line 202, in inference
    driver.get(url)
  File "/opt/anaconda3/envs/cv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/opt/anaconda3/envs/cv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/opt/anaconda3/envs/cv/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 10.000
  (Session info: headless chrome=83.0.4103.116)

有人知道为什么在正确执行一段时间后,Selenium开始为它试图打开的任何url提供超时异常吗?

共有1个答案

杜彦君
2023-03-14

此错误消息...

selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: 10.000

...暗示ChromeDriver无法与浏览上下文(即Chrome浏览器会话)通信。

此错误可能是由于几个原因造成的。其中几个原因和补救措施如下:

chrome_options.add_argument('disable-infobars')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument('--disable-gpu')

>

  • 您可以通过{width}x{height}选择使用更大的视口,例如1920,1080

    chrome_options.add_argument("window-size=1920,1080")
    

    您可以在如何在Selenium Chrome Python中设置窗口大小中找到详细的讨论

    >

  • 要初始化google-chrome-headless而不是chrome_options.add_argument('--headless'),需要使用headless属性,如下所示:

    chrome_options.headless = True
    
      null
    WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#whovaIframeSpeaker")))
    
      null
    • 从呈现器接收消息超时:10.000
    • 如何在chrome驱动程序中处理“无法从渲染器接收消息”?
    • 从呈现器接收消息超时

  •  类似资料:
    • 问题内容: 我正在尝试从某个门户网站获取交易状态,并且在我的Java应用程序中使用了以下chrome设置, 超时从渲染器接收消息:60.000 并且所有待处理的交易都已超时。 会话信息:headless chrome = 68.0.3440.75 驱动程序信息: chromedriver = 2.38 (0) 平台= Linux 2.6.32-696.23.1.el6.x86_64 x86_64)

    • 我试图从一些门户网站获取交易状态,我在我的java应用程序中使用下面的chrome设置 从渲染器接收消息时超时:60.000 所有悬而未决的交易都在超时。 会话信息:headless chrome=68.0.3440.75 驱动程序信息:chromedriver=2.38(0) 平台=Linux 2.6.32-696.23.1。el6。x86_64 x86_64) 我如何处理这个问题,如果发生任何

    • 您好,我在chrome中通过SeleniumWebDriver执行UI测试时遇到以下错误。 81.0.4044.69。 [1586841277.704][严重]:从渲染器接收消息超时:0.100 有人能帮我修一下吗?

    • 在Chrome发布了他们最新的稳定版本(89.0.4389.90),我现在收到这个错误: 硒。常见的例外情况。TimeoutException:消息:超时:从呈现程序接收消息时超时:291.642(会话信息:headless chrome=89.0.4389.90) 我的Python代码是 (我的chromedriver是89.0.4389.23是unic) 有人能帮我修一下吗?

    • 在Jenkins上运行cucumber场景时,有时会遇到以下错误:“org.openqa.selenium.TimeoutException:timeout:timeout从渲染器接收消息:10.000”。由于这个错误,图像、截图根本不生成,如何解决这个问题? 05:51:16org.openqa.selenium.Timeout异常:超时:从渲染器接收消息超时:10.000 05:51:16(会