rdonuk
rdonuk

Reputation: 3956

Chrome browsing windows stays open even after session failed with WebDriverException

I am using selenium 3.141.59 with chrome 79 and chromedriver 79. Randomly I am getting an exception from RemoteWebDriver.get(url); and that session removing from the selenium server. But the Chrome window stays open. Because of that, I am not able to create new sessions with the same user directory. On my every attempt to create a new session, chrome window opens but session creation fails. So those All open Chrome windows causing memory leak! I tried to set timeout and browserTimeout from the server but it didn't help. Any idea what's happening?

I am starting server by:

java -jar -Dselenium.LOGGER.level=ALL selenium-server-standalone-3.141.59.jar -timeout 250 -browserTimeout 300

The exception I am getting randomly:

Caused by: org.openqa.selenium.WebDriverException: java.net.ConnectException: Connection refused (Connection refused)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'myhost', ip: 'myip', os.name: 'Linux', os.arch: 'amd64', os.version: '4.14.154-128.181.amzn2.x86_64', java.version: '1.8.0_201'
Driver info: mypackage.SeleniumHelper$2
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.88, chrome: {chromedriverVersion: 79.0.3945.36 (3582db32b3389..., userDataDir: /var/tmp/username...}, goog:chromeOptions: {debuggerAddress: localhost:35341}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webdriver.remote.sessionid: 66ddc30a30affc4ba52a539bc41...}
Session ID: 66ddc30a30affc4ba52a539bc411ac2c
        at sun.reflect.GeneratedConstructorAccessor1082.newInstance(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_201]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_201]
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) ~[selenium-remote-driver-3.141.59.jar:?]
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) ~[selenium-remote-driver-3.141.59.jar:?]
        at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) ~[selenium-remote-driver-3.141.59.jar:?]
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) ~[selenium-remote-driver-3.141.59.jar:?]
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) ~[selenium-remote-driver-3.141.59.jar:?]
        at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:277) ~[selenium-remote-driver-3.141.59.jar:?]
        ... 65 more

My chrome options:

ChromeOptions options = new ChromeOptions ();
options.addArguments ("user-data-dir=/var/tmp/username");
options.addArguments ("disable-gpu");
options.addArguments ("disable-impl-side-painting");
options.addArguments ("disable-dev-shm-usage");
options.addArguments ("disable-infobars");
options.addArguments ("disable-gpu-sandbox");
options.addArguments ("no-sandbox");
options.addArguments ("disable-accelerated-2d-canvas");
options.addArguments ("disable-accelerated-jpeg-decoding");
options.addArguments ("test-type=ui");
options.addArguments ("no-proxy-server");

Upvotes: 3

Views: 756

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193338

You need to consider a few things:

  • --user-data-dir: Refers to the directory where the browser stores the user profile. So you can't pass any arbitrary value. See: this and this discussion.
  • --disable-gpu: Disables GPU hardware acceleration. If software renderer is not in place, then the GPU process won't launch. However the purpose of the argument --disable-gpu was to enable on platform. It was needed as SwiftShader fails an assert on Windows in headless mode earlier. As you are on os you need to remove it. See: this and this discussion.
  • Ideally, you only add the arguments which are mandatory as per your Test Specifications.
  • Sample minimum code block:

    public class A_Chrome 
    {
        public static void main(String[] args) 
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
            options.setExperimentalOption("useAutomationExtension", false);
            WebDriver driver =  new ChromeDriver(options);
            driver.get("https://www.google.com/");
            driver.quit();
        }
    }
    
  • Finally, when your program raises an exception, the WebDriver instance looses the control of the Browsing Context and both turns into a Zombie process. Hence window stays open.

Upvotes: 3

Renate van Kempen
Renate van Kempen

Reputation: 134

What if you use a try and except function? Where in except, you let the driver close chrome and reopen it again to create a new session?

     try:
       #set your try code here
     except TimeoutException:
       print('Page took too long to load or there was a different problem :(')
       driver.quit()
          try:
            #set your new code here
          except:
            #set your except here

Or you could try to open an new chrome window after the except

Upvotes: 0

Related Questions