Paul Suart
Paul Suart

Reputation: 6713

Selenium.WebDriver.ChromeDriver slow to launch - why?

I've created the simplest-possible NUnit test to initialise a ChromeDriver, and it's taking nearly 6 seconds to run. Most of the time (roughly 4s) is spent with a blank, inactive Chrome window.

Blank Chrome window looks like this

The ChromeDriver.exe window appears almost instantly.

ChromeDriver.exe window

My unit test looks like this:

[Test]
public void Simplest_Possible_Test()
{
    var options = new ChromeOptions { Proxy = null };
    using (new ChromeDriver(options))
    {
        // Do nothing
    }
}

and I'm using these nuget packages:

  <package id="NUnit" version="3.12.0" targetFramework="net47" />
  <package id="Selenium.Support" version="3.141.0" targetFramework="net47" />
  <package id="Selenium.WebDriver" version="3.141.0" targetFramework="net47" />
  <package id="Selenium.WebDriver.ChromeDriver" version="78.0.3904.7000" targetFramework="net47" />

My question is:

Is this slowness expected? Can I do anything to speed it up?

Upvotes: 2

Views: 22836

Answers (5)

Modar Na
Modar Na

Reputation: 918

upgrading the Selenium.WebDriver.ChromeDriver package resolved it for me

I had updated chrome but not the package and the driver did not complain

chrome version 98 driver version 97

Just wanted to share as it took 5 hours of my life:

Upvotes: 1

CharithJ
CharithJ

Reputation: 47510

I am using following singleton IWebDriver instance and it only takes couple of seconds to launch.

public class UiTest : IDisposable
{
    private IWebDriver driver = null;

    protected IWebDriver Driver
    {
        get
        {
            if (driver == null)
            {
                driver = new ChromeDriver(new ChromeOptions{Proxy = null});
                driver.Manage().Window.Maximize();
            }
            return _driver;
        }
    }

    public void Dispose()
    {
        driver?.Dispose();
    }
}

In windows Automatic proxy setup, you can turn off "Automatic detect settings" and see if there is any difference. However, passing null proxy for ChromeOptions has the similar effect, I guess.

enter image description here

Upvotes: 3

Paul Suart
Paul Suart

Reputation: 6713

I have concluded that this is just how long it takes, and have come up with a way to re-use browser windows.

If you start the ChromeDriver via the overload that takes a ChromeDriverService and set EnableVerboseLogging as follows...

var service = ChromeDriverService.CreateDefaultService();
service.EnableVerboseLogging = true;

webDriver = new ChromeDriver(service, options);

... the ChromeDriver window shows verbose info on start-up, including (UNIX-style) timestamps. I'm now satisfied that the time taken to start is simply normal.

Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}) on port 53677
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1573036834.332][INFO]: [dd05361c2fafe521ffd5fb06e6dbe7a3] COMMAND InitSession {
   "capabilities": {
      "firstMatch": [ {
         "browserName": "chrome",
         "goog:chromeOptions": {
            "args": [ "--profile-directory=Default-16031ab478b34fa8838ecc0cb76b2cd1", "--user-data-dir=C:\\Users\\xxx\\AppData\\Local\\Temp\\9f19589865c44ba6aec806fbafc1b561" ]
         }
      } ]
   },
   "desiredCapabilities": {
      "browserName": "chrome",
      "goog:chromeOptions": {
         "args": [ "--profile-directory=Default-16031ab478b34fa8838ecc0cb76b2cd1", "--user-data-dir=C:\\Users\\xxx\\AppData\\Local\\Temp\\9f19589865c44ba6aec806fbafc1b561" ]
      }
   }
}
[1573036834.335][INFO]: Populating Preferences file: {
   "alternate_error_pages": {
      "enabled": false
   },
   "autofill": {
      "enabled": false
   },
   "browser": {
      "check_default_browser": false
   },
   "distribution": {
      "import_bookmarks": false,
      "import_history": false,
      "import_search_engine": false,
      "make_chrome_default_for_user": false,
      "skip_first_run_ui": true
   },
   "dns_prefetching": {
      "enabled": false
   },
   "profile": {
      "content_settings": {
         "pattern_pairs": {
            "https://*,*": {
               "media-stream": {
                  "audio": "Default",
                  "video": "Default"
               }
            }
         }
      },
      "default_content_setting_values": {
         "geolocation": 1
      },
      "default_content_settings": {
         "geolocation": 1,
         "mouselock": 1,
         "notifications": 1,
         "popups": 1,
         "ppapi-broker": 1
      },
      "password_manager_enabled": false
   },
   "safebrowsing": {
      "enabled": false
   },
   "search": {
      "suggest_enabled": false
   },
   "translate": {
      "enabled": false
   }
}
[1573036834.337][INFO]: Populating Local State file: {
   "background_mode": {
      "enabled": false
   },
   "ssl": {
      "rev_checking": {
         "enabled": false
      }
   }
}
[1573036834.348][INFO]: Cannot switch to US keyboard layout - some keys may be interpreted incorrectly
[1573036834.348][INFO]: Launching chrome: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --force-fieldtrials --ignore-certificate-errors --load-extension="C:\Users\PAUL~1.SUA\AppData\Local\Temp\scoped_dir18488_412533912\internal" --log-level=0 --no-first-run --password-store=basic --profile-directory=Default-16031ab478b34fa8838ecc0cb76b2cd1 --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir="C:\Users\xxx\AppData\Local\Temp\9f19589865c44ba6aec806fbafc1b561"

DevTools listening on ws://127.0.0.1:53680/devtools/browser/bacb830a-c373-460e-83e8-10ab7f05d6ce
[1573036834.744][DEBUG]: DevTools HTTP Request: http://localhost:53680/json/version
[1573036835.088][DEBUG]: DevTools HTTP Response: {
   "Browser": "Chrome/78.0.3904.70",
   "Protocol-Version": "1.3",
   "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36",
   "V8-Version": "7.8.279.17",
   "WebKit-Version": "537.36 (@edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5)",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/browser/bacb830a-c373-460e-83e8-10ab7f05d6ce"
}

[1573036835.104][DEBUG]: DevTools HTTP Request: http://localhost:53680/json
[1573036835.122][DEBUG]: DevTools HTTP Response: [ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:53680/devtools/page/C5722CC4576C0C53221985793E2EB260",
   "id": "C5722CC4576C0C53221985793E2EB260",
   "title": "Chrome Automation Extension",
   "type": "background_page",
   "url": "chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/page/C5722CC4576C0C53221985793E2EB260"
}, {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:53680/devtools/page/4B8C0B4FB68B7222E5E34BB28D75A6E4",
   "id": "4B8C0B4FB68B7222E5E34BB28D75A6E4",
   "title": "New Tab",
   "type": "page",
   "url": "chrome://newtab/",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/page/4B8C0B4FB68B7222E5E34BB28D75A6E4"
} ]

[1573036835.131][DEBUG]: DevTools HTTP Request: http://localhost:53680/json
[1573036835.135][DEBUG]: DevTools HTTP Response: [ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:53680/devtools/page/C5722CC4576C0C53221985793E2EB260",
   "id": "C5722CC4576C0C53221985793E2EB260",
   "title": "Chrome Automation Extension",
   "type": "background_page",
   "url": "chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/page/C5722CC4576C0C53221985793E2EB260"
}, {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:53680/devtools/page/4B8C0B4FB68B7222E5E34BB28D75A6E4",
   "id": "4B8C0B4FB68B7222E5E34BB28D75A6E4",
   "title": "New Tab",
   "type": "page",
   "url": "chrome://newtab/",
   "webSocketDebuggerUrl": "ws://localhost:53680/devtools/page/4B8C0B4FB68B7222E5E34BB28D75A6E4"
} ]

[1573036835.152][INFO]: resolved localhost to ["::1","127.0.0.1"]
[1573036837.137][WARNING]: Timed out connecting to Chrome, retrying...
[1573036837.138][INFO]: resolved localhost to ["::1","127.0.0.1"]
[1573036839.141][DEBUG]: DevTools WebSocket Command: Log.enable (id=1) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.142][DEBUG]: DevTools WebSocket Command: DOM.getDocument (id=2) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.144][DEBUG]: DevTools WebSocket Command: Target.setAutoAttach (id=3) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "autoAttach": true,
   "waitForDebuggerOnStart": false
}
[1573036839.145][DEBUG]: DevTools WebSocket Command: Page.enable (id=4) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.146][DEBUG]: DevTools WebSocket Command: Page.enable (id=5) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.146][DEBUG]: DevTools WebSocket Response: Log.enable (id=1) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.147][DEBUG]: DevTools WebSocket Response: DOM.getDocument (id=2) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "root": {
      "backendNodeId": 2,
      "baseURL": "chrome-search://local-ntp/local-ntp.html",
      "childNodeCount": 2,
      "children": [ {
         "backendNodeId": 14,
         "localName": "",
         "nodeId": 2,
         "nodeName": "html",
         "nodeType": 10,
         "nodeValue": "",
         "parentId": 1,
         "publicId": "",
         "systemId": ""
      }, {
         "attributes": [ "lang", "en", "class", "inited" ],
         "backendNodeId": 15,
         "childNodeCount": 4,
         "children": [ {
            "backendNodeId": 16,
            "localName": "",
            "nodeId": 4,
            "nodeName": "#comment",
            "nodeType": 8,
            "nodeValue": " TODO(dbeam): dir=\"ltr\"? ",
            "parentId": 3
         }, {
            "backendNodeId": 17,
            "localName": "",
            "nodeId": 5,
            "nodeName": "#comment",
            "nodeType": 8,
            "nodeValue": " Copyright 2015 The Chromium Authors. All rights reserved.\n     Use of this source code is governed by a BSD-style license that can be\n     found in the LICENSE file. ",
            "parentId": 3
         }, {
            "attributes": [  ],
            "backendNodeId": 18,
            "childNodeCount": 24,
            "localName": "head",
            "nodeId": 6,
            "nodeName": "HEAD",
            "nodeType": 1,
            "nodeValue": "",
            "parentId": 3
         }, {
            "attributes": [ "class", "light-chip win", "style", "background: rgb(255, 255, 255); --logo-color:rgba(238,238,238,1);" ],
            "backendNodeId": 19,
            "childNodeCount": 21,
            "localName": "body",
            "nodeId": 7,
            "nodeName": "BODY",
            "nodeType": 1,
            "nodeValue": "",
            "parentId": 3
         } ],
         "frameId": "4B8C0B4FB68B7222E5E34BB28D75A6E4",
         "localName": "html",
         "nodeId": 3,
         "nodeName": "HTML",
         "nodeType": 1,
         "nodeValue": "",
         "parentId": 1
      } ],
      "documentURL": "chrome-search://local-ntp/local-ntp.html",
      "localName": "",
      "nodeId": 1,
      "nodeName": "#document",
      "nodeType": 9,
      "nodeValue": "",
      "xmlVersion": ""
   }
}
[1573036839.148][DEBUG]: DevTools WebSocket Response: Target.setAutoAttach (id=3) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.148][DEBUG]: DevTools WebSocket Response: Page.enable (id=4) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.148][DEBUG]: DevTools WebSocket Response: Page.enable (id=5) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.149][DEBUG]: DevTools WebSocket Command: Runtime.enable (id=6) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.150][DEBUG]: DevTools WebSocket Event: Runtime.executionContextCreated 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "context": {
      "auxData": {
         "frameId": "04C2466A16DFD233FEE7E6F1A4D5B59C",
         "isDefault": true,
         "type": "default"
      },
      "id": 4,
      "name": "",
      "origin": "chrome-search://most-visited"
   }
}
[1573036839.150][DEBUG]: DevTools WebSocket Event: Runtime.executionContextCreated 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "context": {
      "auxData": {
         "frameId": "B6EA84A25C9E0EFFB88946CC7606D044",
         "isDefault": true,
         "type": "default"
      },
      "id": 3,
      "name": "",
      "origin": "chrome-search://most-visited"
   }
}
[1573036839.151][DEBUG]: DevTools WebSocket Event: Runtime.executionContextCreated 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "context": {
      "auxData": {
         "frameId": "BA8BAD5C705B50773F7EC3B2596AE5B7",
         "isDefault": true,
         "type": "default"
      },
      "id": 2,
      "name": "",
      "origin": "chrome-search://local-ntp"
   }
}
[1573036839.151][DEBUG]: DevTools WebSocket Event: Runtime.executionContextCreated 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "context": {
      "auxData": {
         "frameId": "4B8C0B4FB68B7222E5E34BB28D75A6E4",
         "isDefault": true,
         "type": "default"
      },
      "id": 1,
      "name": "",
      "origin": "chrome-search://local-ntp"
   }
}
[1573036839.151][DEBUG]: DevTools WebSocket Response: Runtime.enable (id=6) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.154][DEBUG]: DevTools WebSocket Command: Page.enable (id=7) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.155][DEBUG]: DevTools WebSocket Response: Page.enable (id=7) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.155][DEBUG]: DevTools WebSocket Command: Runtime.enable (id=8) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.156][DEBUG]: DevTools WebSocket Response: Runtime.enable (id=8) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {

}
[1573036839.157][DEBUG]: DevTools WebSocket Command: Runtime.evaluate (id=9) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "awaitPromise": true,
   "expression": "(function() { // Copyright (c) 2012 The Chromium Authors. All rights reserved.\n// Use of this source code is governed by a BSD-style license that can be\n// found in the LICENSE file.\n\n/**\n * Enum f...",
   "returnByValue": true
}
[1573036839.159][DEBUG]: DevTools WebSocket Response: Runtime.evaluate (id=9) 4B8C0B4FB68B7222E5E34BB28D75A6E4 {
   "result": {
      "type": "object",
      "value": {
         "status": 0,
         "value": 1
      }
   }
}
[1573036839.160][INFO]: [dd05361c2fafe521ffd5fb06e6dbe7a3] RESPONSE InitSession {
   "capabilities": {
      "acceptInsecureCerts": false,
      "browserName": "chrome",
      "browserVersion": "78.0.3904.70",
      "chrome": {
         "chromedriverVersion": "78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800})",
         "userDataDir": "C:\\Users\\xxx\\AppData\\Local\\Temp\\9f19589865c44ba6aec806fbafc1b561"
      },
      "goog:chromeOptions": {
         "debuggerAddress": "localhost:53680"
      },
      "networkConnectionEnabled": false,
      "pageLoadStrategy": "normal",
      "platformName": "windows nt",
      "proxy": {

      },
      "setWindowRect": true,
      "strictFileInteractability": false,
      "timeouts": {
         "implicit": 0,
         "pageLoad": 300000,
         "script": 30000
      },
      "unhandledPromptBehavior": "dismiss and notify"
   },
   "sessionId": "dd05361c2fafe521ffd5fb06e6dbe7a3"
}

Upvotes: 2

Ayberk
Ayberk

Reputation: 560

There are multiple reasons where Chrome driver is loading very slow. I see you've already found the first one which is the proxy setting. The second one is the bitness of the driver. We've experienced 64-bit version of ChromeDriver was very slow. You can try using other versions of the Chrome Driver.

We are using this version:

<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="2.45.0" />

This is not about increasing the load time of the Chrome Driver but how you approach it; When you execute the following piece of code, it opens two executables. Your ChromeDriver.exe and chrome.exe.

    var options = new ChromeOptions { Proxy = null };
    using (new ChromeDriver(options))
    {
        // Do nothing
    }

If you are don't have different Chrome options for each test, I strongly recommend you to initialize your driver for only once for your all tests and manage your chrome.exe with Quit(), Close() etc. Please see [Setup] or [OnetimeSetup] attributes depending on your version of NUnit. Then your total test run execution time will decrease significantly.

Upvotes: 0

Javier Silva Ort&#237;z
Javier Silva Ort&#237;z

Reputation: 2982

A few months ago, I (and others), built a web bot (don't ask what for :-) ). The target surface was a particular website. For preparations, we replicated as much as possible the target website in an offline LAN environment. Among other tools, we used Selenium to run Chrome, and startup time was ~4-6 seconds, the gig was a Intel Core i5 ~3 GHz 8GB RAM Windows 10 Pro and had another similar computer, a laptop, main difference being it was running Ubuntu 18.04, about same startup time.

C# is not the issue here. After noticing that delay (which is not a big deal, at least for me), considering the offline setting (no requests flying around the Internet), and the different computer builds, I dare to say it's the startup time it takes, that's all, no monkey business.

Faster startup times could be achieved by using a higher end gig with faster memory, faster CPU, faster buses, SSD drives, etc...

Upvotes: 2

Related Questions