Selenium 超时从渲染器接收消息

昨天 Chrome 发布最新版本(64.0.3282)后,我偶尔会收到这个错误:

超时接收来自渲染器的消息: 600.000

我在一个 Docker 容器中运行了大约2,000个硒测试,我发现这个故障的发生率大约是百分之一。据我所知,没有可重复的步骤——每次迭代失败的测试都是不同的。我更新了最新的 ChromeDriver (2.35) ,但是似乎没有任何效果。我以前使用的是 Selenium 2.41,但是已经更新到最新的版本(3.8.1) ,希望它可能会有所帮助... 但是它没有。我完全不知道为什么会发生这种事。有人注意到这个吗?可能是 Chrome 最新版本的 bug 吗?

提前感谢您所能提供的任何帮助。

159591 次浏览

It looks like there was an issue with the newest Chrome release. Without the disable-gpu Chromeoption set, the renderer will occasionally timeout. The workaround until Google fixes this (if they do fix it at all) is to add the --disable-gpu attribute to the ChromeOptions.

EDIT: This reduced the frequency of occurrences, but it is still happening.

I was seeing the Timed out receiving message from renderer: aka Net::ReadTimeout issue 100% of the time in a Cucumber test running in a Jenkins build env after the docker selenium/standalone-chrome image was updated in late Jan 2018. Adding the --disable-gpu attribute to the ChromeOptions did not fix it for me, but adding the --disable-browser-side-navigation option fixed it 100%. I found that recommendation here: https://bugs.chromium.org/p/chromedriver/issues/detail?id=2239#c10

it said there are several workarounds to this issue:

  • A fix is in Chrome v65, which is currently available in beta. This is the best option if you can use beta builds.

  • Add --disable-browser-side-navigation switch to Chrome command line.

  • Use ChromeDriver 2.33, which automatically uses --disable-browser-side-navigation.

Check for JS Runtime

First verify you aren't executing / eval()ing a lot of javascript. That can cause a timeout.

Check Version Compatibility

First, verify your versions of:

  • Selenium
  • JDK
  • ChromeDriver
  • Chrome

are all compatible. Good luck doing this because there is no single place that documents it, AND selenium software isn't smart enough to do a quick check (it should)

Check Driver Initialization

Add this cryptic block of code, what I like to call the "Ever Growing List of Useless Arguments" chromedriver requires

up to date from every issue ever reported on stack overflow as of: September 2018

// ChromeDriver is just AWFUL because every version or two it breaks unless you pass cryptic arguments
//AGRESSIVE: options.setPageLoadStrategy(PageLoadStrategy.NONE); // https://www.skptricks.com/2018/08/timed-out-receiving-message-from-renderer-selenium.html
options.addArguments("start-maximized"); // https://stackoverflow.com/a/26283818/1689770
options.addArguments("enable-automation"); // https://stackoverflow.com/a/43840128/1689770
options.addArguments("--headless"); // only if you are ACTUALLY running headless
options.addArguments("--no-sandbox"); //https://stackoverflow.com/a/50725918/1689770
options.addArguments("--disable-dev-shm-usage"); //https://stackoverflow.com/a/50725918/1689770
options.addArguments("--disable-browser-side-navigation"); //https://stackoverflow.com/a/49123152/1689770
options.addArguments("--disable-gpu"); //https://stackoverflow.com/questions/51959986/how-to-solve-selenium-chromedriver-timed-out-receiving-message-from-renderer-exc
driver = new ChromeDriver(options);


//This option was deprecated, see https://sqa.stackexchange.com/questions/32444/how-to-disable-infobar-from-chrome
//options.addArguments("--disable-infobars"); //https://stackoverflow.com/a/43840128/1689770

Sources:

I had this issue today, with Chrome: Version 73.0.3683.86 (Official Build) (64-bit). For me it was failing on the timeouts on Jenkins builds, and was fine locally, see the following Chrome options that helped me to overcome that issue (ChromeDriver at this time: version - 73.0.3683.68):

ChromeOptions options = new ChromeOptions();
options.addArguments("enable-automation");
options.addArguments("--headless");
options.addArguments("--window-size=1920,1080");
options.addArguments("--no-sandbox");
options.addArguments("--disable-extensions");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--disable-gpu");
options.setPageLoadStrategy(PageLoadStrategy.NORMAL);

I was seeing issues going from Chrome 72 to 73 and was getting the error message:

Timed out receiving message from renderer: 600.000

I was getting the error only when i was running tests on Jenkins (tests were running fine on my local development machine) which i found rather odd.

I tried Firefox and no issues found so this narrowed it down to Chrome. After looking through the Chromium issue tracker i found Issue 946441: Chromedriver: Timed out receiving message from renderer error for Selenium+Chrome+Jenkins(user SYSTEM)

As this was a renderer issue, i tried running tests in headless mode which resolved the issue.

I encountered the same issue while triggering execution from Jenkins. I played around a bit and found that only adding the below chrome option makes thing work:

options.addArguments("--no-sandbox");

If your website is https, and having trouble with chromedriver on timedout issue, use

option.addArguments("enable-features=NetworkServiceInProcess")

If the above doesn't work, use

option.addArguments("disable-features=NetworkService")

instead

Credit goes to https://groups.google.com/forum/#!topic/chromedriver-users/yHuW_Z7tdy0

In our case, we found the problem was a performance issue because the screenshot taken was huge but it happened because we realized the screenshot resolution of the screenshots created by Chrome was double than the specified in code for calling the chromedriver, for example, if we specified width 1024px and height 2000px in the screenshot, it was creating an image of 2048px width and 4000px height so that is why was taking too much for rendering and the timeout breaks the process, in cases when the process finished (after long wait) it was creating a heavy screenshot images. We found it was an option (problem) of Chrome for supporting retina devices which increase the resolution of the screenshot, so, we forced using a flag to deactivate the option and render the original resolution configured and it works well, taking between 8 seconds to 13 seconds to create the screenshot (depending on the content of the page) and the image size is less than at the begenning. This is the flag into the ChromeOptions object:

options.addArguments("--force-device-scale-factor=1");

I know the question as about Chromedriver, but for anyone like me who isn't specifically testing on Chrome and just needs a working headless browser in Selenium: switch to Firefox (Geckodriver). I set a single option and forgot all about these Chromedriver bugs and rendering problems:

from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
browser = webdriver.Firefox(options=options)

It just works (tm).

In order to execute chrome test cases parallel in a headless mode using jenkins


options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--aggressive-cache-discard");
options.addArguments("--disable-cache");
options.addArguments("--disable-application-cache");
options.addArguments("--disable-offline-load-stale-cache");
options.addArguments("--disk-cache-size=0");
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--no-proxy-server");
options.addArguments("--log-level=3"); options.addArguments("--silent"); options.addArguments("--disable-browser-side-navigation"); options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
options.setProxy(null);

Root cause: Whenever you are loading some page with the help of selenium driver,  then driver script wait till page is completely loaded. But sometime webdriver takes more time to load page, in that case you will see TimeoutException exception in your console.

Solution: When Page Loading takes too much time for wait so we will wait for the DOMContentLoaded event with page load strategy. This page load strategy is called Eager. A small definition of available all 3 pageload strategies.

1. normal: This strategy causes Selenium to wait for the full page loading (html content and sub resources downloaded and parsed).

2. eager : This strategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).

3. none : This strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).

NOTE : By default, when Selenium loads a page, it follows the normal pageLoadStrategy.

Code snippet without using Pageload strategy (Or Normal as used by selenium by default)

System.setProperty("webdriver.chrome.driver", "C:\\Users\\...\\LatestDriver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.name("q")));
el.click();
List <WebElement> allLinks = driver.findElements(By.tagName("a"));
System.out.println(allLinks.size());
driver.quit();

Console Output:

Starting ChromeDriver 80.0.3987.16 (320f6526c1632ad4f205ebce69b99a062ed78647-refs/branch-heads/3987@{#185}) on port 41540 Only local connections are allowed. Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code. Feb 11, 2020 10:22:12 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C [1581412933.937][SEVERE]: Timed out receiving message from renderer: 0.100 [1581412934.066][SEVERE]: Timed out receiving message from renderer: 0.100 [1581412934.168][SEVERE]: Timed out receiving message from renderer: 0.100 [1581412934.360][SEVERE]: Timed out receiving message from renderer: 0.100 [1581412934.461][SEVERE]: Timed out receiving message from renderer: 0.100 [1581412934.618][SEVERE]: Timed out receiving message from renderer: 0.100 [1581412934.719][SEVERE]: Timed out receiving message from renderer: 0.100 [1581412934.820][SEVERE]: Timed out receiving message from renderer: 0.100 [1581412934.922][SEVERE]: Timed out receiving message from renderer: 0.100 [1581412935.097][SEVERE]: Timed out receiving message from renderer: 0.100 21

With PageLoad Strategy - Eager - Code Snippet:

System.setProperty("webdriver.chrome.driver", "C:\\Users\\...\\LatestDriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
WebDriver driver=new ChromeDriver(options);
driver.get("http://www.google.com");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.name("q")));
el.click();
List <WebElement> allLinks = driver.findElements(By.tagName("a"));
System.out.println(allLinks.size());
driver.quit();

Console Output:

Starting ChromeDriver 80.0.3987.16 (320f6526c1632ad4f205ebce69b99a062ed78647-refs/branch-heads/3987@{#185}) on port 1175 Only local connections are allowed. Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code. Feb 11, 2020 10:29:05 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C 21

You need to disable the ChromeDriverService loggers.

Add the following method to whichever class is creating the driver,

and make sure call it once before creating any driver instances:

import org.openqa.selenium.chrome.ChromeDriverService;
import java.util.logging.Level;
import java.util.logging.Logger;


public static void disableSeleniumLogs() {
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
}

Sounds silly, but when the loops just doesn't end, try and check your internet connection.

You can overcome these timeout error by running Chromedriver executable file in Silent mode

System.setProperty("webdriver.chrome.driver","driver_path");
System.setProperty("webdriver.chrome.silentOutput", "true");

Just addon hope this will help someone:If you are coding in python3

You are getting error like Instance of 'Options' has no 'addArguments' memberpylint(no-member) while using options.addArguments("--xxx") then you need to change addArguments to add_arguments
Like: options.add_argument("--xxxx")

I found that in my case, the sporadic failures were because CPU resources would go up and down sporadically on the server which changes the time it takes to render elements or execute JS on a page. So, the ultimate solution for me was to simply increase the timeouts as follows:

// Driver constants and methods
const STD_TIMEOUT = 100000


let setDriverTimeout = async (timeout) => {
await global.driver.manage().setTimeouts({
implicit: timeout,
pageLoad: timeout,
script: timeout
})
}

Here, if I use STD_TIMEOUT that is too small, I will get more of the error "timed out receiving message from renderer".

Also, as a side-note, I can trigger this to happen even more frequently by increasing the CPU throttling rate as such:

  global.driver = await new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build()


await driver.sendDevToolsCommand(
'Emulation.setCPUThrottlingRate', {
rate: 1800
}
);

With a high rate such as 1800, the renderer will timeout very often.

First of all the other answers have been of great help and as everyone, I too have been struggling to make the chrome webdriver work, using the endless list of arguments that it requires. Below are some of my observations after breaking my head over this for a while:

The Timeout error is received due to manually setting the timeout of the webdriver. driver.set_page_load_timeout(30) We can skip this line and resolve the error, but then the webdriver would run indefinitely if the website keeps on loading and hence is always recommended to use.

First of all, make sure the chrome driver and chrome are on the stable recent cross-compatible versions. (most common error) Since I use docker for my use-case, I let my code handle to download the latest stable versions of both chrome and chromedriver during docker build, to rule out compatibility issues. Refer here for a simple explanation of the same.

Secondly, the below arguments helped me run the chromedriver as headless without any issues. I'm surprised the arguments aren't well documented anywhere. Found good documentation here

--no-sandbox
window-size=1920,1080
--ignore-certificate-errors
--ignore-ssl-errors
--disable-dev-shm-usage
--disable-gpu")
--log-level=3")
enable-features=NetworkServiceInProcess
disable-features=NetworkService

And thirdly, after the above configurations, the issue I faced was it was working for me in a docker container when run on windows, but failed to run on Linux/Kubernetes. The reason being, for Linux, it requires a PyVirtualDisplay to work. Hence adding this below piece of code before using the webdriver resolved all issues.

from pyvirtualdisplay import Display
display = Display(visible=0, size=(1024, 768))
display.start()

THIS WORKS After googling through many many threads, I finally got the solution to such problems.

While most of the solutions above have pointed out the problem correctly which is arising due to bug in Chrome updates, follow below solution to get it fixed-

  1. Download and use the latest ChromeDriver- compatible with the version of Chrome
  2. Download the latest Selenium Driver from https://www.selenium.dev/ and execute the jar file on your system once.
  3. Run the you code again to see the magic

Seems Like browser is not loading the URL due to network issue or you quited the driver with driver.quit() in previous execution

v93 broken my code, i tried so many option finally, following code is working fine for me.

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--disable-gpu");
WebDriverManager.chromedriver().forceDownload().setup();
webDriver = new ChromeDriver(chromeOptions);

I had the same problem during jenkins job run. The fix which was working for me is to setup proxies in .bash_profile under jenkins user:

sudo su -s /bin/bash jenkins
nano ~/.bash_profile
...your proxy
source ~/.bash_profile