Home > Software design >  Selenium ChromeDriver gives an errors after one hour of running
Selenium ChromeDriver gives an errors after one hour of running

Time:12-30

I'm new with selenium and I try to build a bot to do some logic on a specific website. I use C# .NET 4.6, Chrome v.108 and latest Selenium and chrome driver from Nuget. The flow itself can be less then a few seconds, and if it's failed, I want it to try again, until it will accomplish it.

Potentially, it can runs a lot of hours (or even a day or two) until it will success. However, after an hour or two, it start to throw A LOT exceptions, the most common is these three:

disconnected: Unable to receive message from renderer
  (failed to check if window was closed: disconnected: Unable to receive message from renderer)
  (Session info: headless chrome=108.0.5359.125)*

An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL ___localhostURL___ The exception message was: An error occurred while sending the request.

The HTTP request to the remote WebDriver server for URL ___localhostURL___ timed out after 60 seconds.

I tried to run my bot, both on my machine and on a docker as well, and I got the same errors after a while.

I search these errors over the net and tried some solutions (most of them was to add some arguments to the ChromeOptions) but actually they didn't help me so much and I still get these errors.

This is my code:

private void Func()
{
    var status = Status.Failed;
    ChromeDriver driver = null;

    var options = new ChromeOptions();
    options.AddArgument("--headless");
    options.AddArgument("--user-agent='Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 640 XL LTE) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.10166'");
    options.AddArgument("--no-sandbox");
    options.AddArgument("--disable-gpu");
    options.AddArgument("--disable-dev-shm-usage");
    driver = new ChromeDriver(path/to/chromedrive.exe, options);
    //driver = new RemoteWebDriver(new Uri(dockerUrl), options);

    do
    {
        try
        {
            driver.Navigate().GoToUrl(PageUrl);
            status = Func2(driver);
        }
        catch (Exception ex)
        {
            WriteException(ex);
        }
    } while (status == Status.Failed);

    driver?.Quit();
}
  • User agent argument is because the specific website block headless browser and this is how I found how to bypass this block.
  • All other arguments is a solutions I found to fix the problems above which didn't help so much.
  • I know I can use driver.Navigate().Refresh(); instead of driver.Navigate().GoToUrl(PageUrl); every iteration, but with Refresh() it doesn't do what I want since it refresh the current page and not the main one.
  • Both with RemoteWebDriver (will run the bot on a docker) and ChromeDriver I get the same errors.
  • I tried without path to chromedriver.exe (actually I didn't know I need this one at all since it works the same without it as well) but with or without path to chromedriver.exe it still get these errors.
  • These errors raise on the GoToUrl line

Any insights or tips what to change or what to check to fix my problems?
Thanks

CodePudding user response:

Apparently, it all was because Sleep-mode on my laptop.
When I change it to "Never" it seems like it fixed and run more then 24 hours without any issues.

  • Related