2

Context:

  • Windows 10 Home;
  • Visual Studio 2015 Community;
  • C#;
  • NSSM;
  • ChromeDriver 2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129);
  • Nuget package Selenium.WebDriver.2.53.1;
  • Nuget package Selenium.WebDriver.ChromeDriver.2.23.0.1

I have used NSSM to create a Windows Service wrapping chromedriver.exe, with the following settings:

Path: c:\bin\chromedriver.exe
Startup Directory: c:\bin
Arguments: --port=12942 --log-path=c:\temp\chromedriver.log --verbose 
Service Name: ChromeDriverService

In VS2015Community I've created a C# app as follows:

var uri = new Uri("http://localhost:12942");
DesiredCapabilities dc = DesiredCapabilities.Chrome();
ChromeOptions options = new ChromeOptions();            
options.BinaryLocation = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
options.LeaveBrowserRunning = true;
dc.IsJavaScriptEnabled = true;            
IWebDriver driver = new RemoteWebDriver(uri, dc);

When I step through the program, I get a The HTTP request to the remote WebDriver server for URL http://localhost:12942/session timed out after 60 seconds. on the new RemoteWebDriver line.

In the c:\temp\chromedriver.log I get the following:

[44.717][INFO]: COMMAND InitSession {
   "desiredCapabilities": {
      "browserName": "chrome",
      "javascriptEnabled": true,
      "platform": "ANY",
      "version": ""
   }
}
[66.168][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,
      "show_welcome_page": 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
   }
}
[66.171][INFO]: Populating Local State file: {
   "background_mode": {
      "enabled": false
   },
   "ssl": {
      "rev_checking": {
         "enabled": false
      }
   }
}
[68.099][INFO]: Can not set to US keyboard layout - Some keycodes may beinterpreted incorrectly
[68.099][INFO]: Launching chrome: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-background-networking --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --ignore-certificate-errors --load-component-extension="C:\WINDOWS\TEMP\scoped_dir5436_17406\internal" --log-level=0 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12772 --safebrowsing-disable-auto-update --test-type=webdriver --use-mock-keychain --user-data-dir="C:\WINDOWS\TEMP\scoped_dir5436_20664" data:,
[68.127][DEBUG]: DevTools request: http://localhost:12772/json/version
[70.154][DEBUG]: DevTools response: {

   "Browser": "Chrome/52.0.2743.116",

   "Protocol-Version": "1.1",

   "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",

   "WebKit-Version": "537.36 (@9115ecad1cae66fd5fe52bd9120af643384fd6f3)"

}


[70.155][DEBUG]: DevTools request: http://localhost:12772/json
[70.168][DEBUG]: DevTools response: [  ]

... a minute's worth of the above two lines removed ...

[128.264][INFO]: RESPONSE InitSession unknown error: unable to discover open pages
[128.264][DEBUG]: Log type 'driver' lost 0 entries on destruction
[128.264][DEBUG]: Log type 'browser' lost 0 entries on destruction

I have a distant memory of having successfully talked to an NSSM-wrapped chromedriver.exe earlier this year. I can't find the code now. What am I missing here?

bugmagnet
  • 7,631
  • 8
  • 69
  • 131

1 Answers1

3

You're using RemoteWebDriver as your client. The address you give should be the Selenium Grid hub URL, not the chromedriver URL.

You should either configure Selenium Grid to know about chromedriver, or switch from RemoteWebDriver to ChromeDriver as your client.

ChromeDriver connects to a service provided by chromedriver.exe on any machine, which may be where the confusion about RemoteWebDriver comes in. RemoteWebDriver means "any browser, anywhere, as chosen by a Selenium Grid hub using criteria that you provide". ChromeDriver means "any Chrome, in one specific location chosen by you".

If you switch to ChromeDriver, this code works well:

var service = ChromeDriverService.CreateDefaultService();
// service.EnableVerboseLogging = true;
service.Port = 12942;
var options = new ChromeOptions
{
    Proxy = new Proxy {Kind = ProxyKind.Direct}
};

return new ChromeDriver(service, options);

There are lots more options you can provide to the service.

Aside: note that you configure the service to customize how ChromeDriver talks to chromedriver.exe; you configure ChromeOptions to customize how Chrome talks to the internet (or to your webapp).

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • I'm not using Selenium Grid (haven't read the friendly manual about that yet.) So is that approach hooking into the NSSM-wrapped ChromeDriverService Windows service or bypassing it in favour of an explicit Selenium ChromeDriverService? – bugmagnet Aug 11 '16 at 02:48
  • Is it appropriate to add service.PortServerAddress = "http://localhost"; service.Port = 12942; ? – bugmagnet Aug 11 '16 at 02:51
  • 1
    You don't need `PortServerAddress` if you're working on localhost. Updating the port is good, unless you're using the default. – Paul Hicks Aug 11 '16 at 03:02
  • However, trying to put Port into ChromeOptions doesn't work. – bugmagnet Aug 11 '16 at 03:05
  • Running chromedriver.exe through NSSM is fine. Using `ChromeDriver` does not eliminate the requirement for chromedriver.exe. The naming is unfortunate, because `ChromeDriver` the class is a client, and chromedriver.exe is the server for the client. It is also a client, in that it sends requests to a running chrome instance. But that's outside of Selenium's problem domain; Selenium "stops" at the json service provided by chromedriver.exe. – Paul Hicks Aug 11 '16 at 03:06
  • Oops my bad. Port is for configuring the service, not the options. Onesec, I'll update the answer. – Paul Hicks Aug 11 '16 at 03:06