簡體   English   中英

Selenium 在執行一段時間后為所有網站提供“超時接收來自渲染器的消息”

[英]Selenium gives “Timed out receiving message from renderer” for all websites after some execution time

我有一個應用程序,我需要一個長時間運行的Selenium web 驅動程序實例(我在無頭模式下使用Chrome 驅動程序 83.0.4103.39 )。 基本上,該應用程序不斷地從隊列中提取 url 數據,並將提取的 url 提供給 Selenium,這應該在網站上執行一些分析。 其中許多網站可能已關閉、無法訪問或損壞,因此我將頁面加載超時設置為 10 秒以避免 Selenium 永遠等待頁面加載。
我在這里遇到的問題是,經過一段時間的執行時間(比如說 10 分鍾)Selenium 開始為每個 url 提供Timed out receiving message from renderer 最初它工作正常,它正確打開了好的網站並在壞的網站上超時(網站無法加載),但一段時間后它開始在所有內容上超時,即使是應該正確打開的網站(我已經檢查過,它們在 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 提供超時異常?

此錯誤消息...

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

...意味着ChromeDriver無法與瀏覽上下文通信,即Chrome 瀏覽器session。


深潛

由於多種原因,可能會出現此錯誤。 其中一些原因和補救措施如下:

  • disable-infobars--enable-automation幾乎是類似的,並且disable-infobars被維護。 --enable-automation將滿足您的目的。 所以你需要放棄:

     chrome_options.add_argument('disable-infobars')

您可以在 Chrome v76 中的無法隱藏“Chrome 正在由自動化軟件控制”信息欄中找到詳細討論

  • --enable-automation仍然是一個experimental_option選項,所以你需要:

     chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])

您可以在How can I use setExperimentalOption through Options using FirefoxDriver in Selenium IDE?

  • 如果您打算使用--enable-automation您還需要使用useAutomationExtension

     chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) chrome_options.add_experimental_option('useAutomationExtension', False)
  • --disable-gpu不再需要,因此您需要刪除:

     chrome_options.add_argument('--disable-gpu')

您可以在 Python Selenium: Disable GPU vs Headless 中的 Chrome 選項中找到詳細討論

  • 您可以通過{width}x{height}選擇使用更大的視口,例如1920, 1080

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

您可以在How to set window size in Selenium Chrome Python中找到詳細討論

  • 要啟動而不是chrome_options.add_argument('--headless')您需要使用headless屬性,如下所示:

     chrome_options.headless = True

您可以在如何配置 ChromeDriver 通過 Selenium 以 Headless 模式啟動 Chrome 瀏覽器中找到詳細討論?

  • 由於您已經枚舉了所有元素,值得一提的是,您不能switch_to所有<iframe> / <frame>因為其中一些可能將樣式屬性值設置為display: none; .

你可以在Expected condition failed: waiting for element to be clickable for element contains style="display: none;"中找到詳細討論

您可以在以下位置找到一些相關的討論:


參考

您可以在以下位置找到一些有關超時從渲染器接收消息的相關詳細討論:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM