首页 > 解决方案 > python selenium "Timed out receiving message from renderer" when getting screenshot

问题描述

OPS! I am aware there is a lot of "similarish" questions. But this is not them.

This is not duplicate as @DebanjanB is implying: 1) The chrome gets the webpage without any problem as shown in the example and explained in the text. 2)The timeout only happens only on "get screenshot" either as png or base64. 3) Which is also happens only with some specific pages that have long pages. 4) This is Python and not java so upgrading java won't help. 5) ChromeDriver is on v. 2.45 not "current 2.38" 6) Selenium is on v. 3.14.0 not 3.11

Scrips bellow loads fine and gets HTML, its work with most of pages, but in case of https://medici.md/terms-of-service/ the screenshot rendering times out. I do realize that one of the reason can be due to the size of the screenshot

Browser required_width 1200 , required_height 36417

so I wonder is there is a way to overcome this limitation. Clearly, setting the timeout on the get page does not work as this is an internal issue.

import os
import uuid
import base64
import datetime

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities


def save_to_png(byte_code, filename):
    with open(filename, "wb") as fh:
        fh.write(base64.b64decode(byte_code))


def make_browser_2():
    # config browser
    chrome_options = Options()
    _tmp_folder = '/tmp/{}'.format(uuid.uuid4())

    if not os.path.exists(_tmp_folder):
        os.makedirs(_tmp_folder)

    if not os.path.exists(_tmp_folder + '/user-data'):
        os.makedirs(_tmp_folder + '/user-data')

    if not os.path.exists(_tmp_folder + '/data-path'):
        os.makedirs(_tmp_folder + '/data-path')

    if not os.path.exists(_tmp_folder + '/cache-dir'):
        os.makedirs(_tmp_folder + '/cache-dir')

    caps = DesiredCapabilities().CHROME
    caps["pageLoadStrategy"] = "normal"
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-setuid-sandbox')
    chrome_options.add_argument('--dns-prefetch-disable')
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument("--window-size=1200x900")
    chrome_options.add_argument('--user-data-dir={}'.format(_tmp_folder + '/user-data'))
    chrome_options.add_argument('--hide-scrollbars')
    chrome_options.add_argument('--enable-logging')
    chrome_options.add_argument('--log-level=0')
    chrome_options.add_argument('--v=99')
    chrome_options.add_argument('--single-process')
    chrome_options.add_argument('--data-path={}'.format(_tmp_folder + '/data-path'))
    chrome_options.add_argument('--ignore-certificate-errors')
    chrome_options.add_argument('--homedir={}'.format(_tmp_folder))
    chrome_options.add_argument('--disk-cache-dir={}'.format(_tmp_folder + '/cache-dir'))
    chrome_options.add_argument('--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36')
    chrome_options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
    return webdriver.Chrome(desired_capabilities=caps, options=chrome_options)


def get_image_fullpage_screenshot(browser):
    required_width = browser.execute_script('return document.body.parentNode.scrollWidth')
    required_height = browser.execute_script('return document.body.parentNode.scrollHeight')
    browser.set_window_size(required_width, required_height)
    # yes, this is huge, so what?!
    print("Browser required_width %d , required_height %d" % (required_width, required_height))
    # driver.save_screenshot(path)  # has scrollbar
    body_element = browser.find_element_by_tag_name('body')
    # body_element.screenshot(filename)  # avoids scrollbar
    try:
        b64 = body_element.screenshot_as_base64
    except Exception as e:
        # got error try to save as screenshot
        print("Error {}".format(e))
        browser.save_screenshot("on-error-screenshot.png")
    return b64


if __name__ == "__main__":
    url1 = "https://medici.md/terms-of-service/"

    browser = make_browser_2()
    now = datetime.datetime.now()

    filename = 'screenshot-{}-{}.png'.format(
        now.strftime('%Y%m%d'),
        now.strftime('%H%M%S')
    )
    browser.get(url1)
    print(browser.title)
    print(browser.page_source)
    filename = 'screenshot-{}-{}-1.png'.format(
        now.strftime('%Y%m%d'),
        now.strftime('%H%M%S')
    )
    # this works
    browser.save_screenshot("screenshot.png")
    #buhuuu
    b64_data = get_image_fullpage_screenshot(browser)
    save_to_png(b64_data, filename)

Error:

Browser required_width 1200 , required_height 36417
Error Message: timeout: Timed out receiving message from renderer: 10.000
  (Session info: headless chrome=73.0.3667.0)
  (Driver info: chromedriver=2.45.615355 (d5698f682d8b2742017df6c81e0bd8e6a3063189),platform=Mac OS X 10.14.1 x86_64)

Traceback (most recent call last):
  File "selenium_error.py", line 67, in get_image_fullpage_screenshot
    b64 = body_element.screenshot_as_base64
  File "/automation/ve/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 557, in screenshot_as_base64
    return self._execute(Command.ELEMENT_SCREENSHOT)['value']
  File "/automation/ve/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "/automation/ve/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/automation/ve/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=73.0.3667.0)
  (Driver info: chromedriver=2.45.615355 (d5698f682d8b2742017df6c81e0bd8e6a3063189),platform=Mac OS X 10.14.1 x86_64)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "selenium_error.py", line 95, in <module>
    b64_data = get_image_fullpage_screenshot(browser)
  File "selenium_error.py", line 71, in get_image_fullpage_screenshot
    browser.save_screenshot("on-error-screenshot.png")
  File "/automation/ve/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 1055, in save_screenshot
    return self.get_screenshot_as_file(filename)
  File "/automation/ve/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 1032, in get_screenshot_as_file
    png = self.get_screenshot_as_png()
  File "/automation/ve/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 1064, in get_screenshot_as_png
    return base64.b64decode(self.get_screenshot_as_base64().encode('ascii'))
  File "/automation/ve/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 1074, in get_screenshot_as_base64
    return self.execute(Command.SCREENSHOT)['value']
  File "/automation/ve/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/automation/ve/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: 5.718
  (Session info: headless chrome=73.0.3667.0)
  (Driver info: chromedriver=2.45.615355 (d5698f682d8b2742017df6c81e0bd8e6a3063189),platform=Mac OS X 10.14.1 x86_64)

标签: python-3.xseleniumselenium-webdriver

解决方案


推荐阅读