针对 ChromeDriver 的 Windows 服务的 Selenium RemoteWebDriver
Selenium RemoteWebDriver against a Windows Service of ChromeDriver
上下文:
- Windows10家;
- Visual Studio 2015 社区;
- C#;
- NSSM;
- ChromeDriver 2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129);
- Nuget 包 Selenium.WebDriver.2.53.1;
- Nuget 包Selenium.WebDriver.ChromeDriver.2.23.0.1
我已经使用 NSSM 创建了一个 Windows 服务包装 chromedriver.exe,设置如下:
Path: c:\bin\chromedriver.exe
Startup Directory: c:\bin
Arguments: --port=12942 --log-path=c:\temp\chromedriver.log --verbose
Service Name: ChromeDriverService
在 VS2015Community 中,我创建了一个 C# 应用程序,如下所示:
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);
当我单步执行程序时,我在 new RemoteWebDriver
行上得到一个 The HTTP request to the remote WebDriver server for URL http://localhost:12942/session timed out after 60 seconds.
。
在 c:\temp\chromedriver.log
我得到以下信息:
[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
我对今年早些时候成功与 NSSM 包装的 chromedriver.exe 交谈有着遥远的记忆。我现在找不到代码了。我在这里错过了什么?
您正在使用 RemoteWebDriver
作为您的客户端。您提供的地址应该是 Selenium Grid hub URL,而不是 chromedriver URL.
您应该 configure Selenium Grid 了解 chromedriver,或者从 RemoteWebDriver
切换到 ChromeDriver
作为您的客户端。
ChromeDriver
连接到任何机器上 chromedriver.exe 提供的服务,这可能是关于 RemoteWebDriver
的混淆所在。RemoteWebDriver
意味着 "any browser, anywhere, as chosen by a Selenium Grid hub using criteria that you provide". ChromeDriver
表示 "any Chrome, in one specific location chosen by you".
如果切换到 ChromeDriver
,此代码运行良好:
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);
您可以为服务提供更多选项。
旁白:请注意,您将服务配置为自定义 ChromeDriver
与 chromedriver.exe 的通信方式;您配置 ChromeOptions
以自定义 Chrome 与互联网(或您的网络应用程序)对话的方式。
上下文:
- Windows10家;
- Visual Studio 2015 社区;
- C#;
- NSSM;
- ChromeDriver 2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129);
- Nuget 包 Selenium.WebDriver.2.53.1;
- Nuget 包Selenium.WebDriver.ChromeDriver.2.23.0.1
我已经使用 NSSM 创建了一个 Windows 服务包装 chromedriver.exe,设置如下:
Path: c:\bin\chromedriver.exe
Startup Directory: c:\bin
Arguments: --port=12942 --log-path=c:\temp\chromedriver.log --verbose
Service Name: ChromeDriverService
在 VS2015Community 中,我创建了一个 C# 应用程序,如下所示:
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);
当我单步执行程序时,我在 new RemoteWebDriver
行上得到一个 The HTTP request to the remote WebDriver server for URL http://localhost:12942/session timed out after 60 seconds.
。
在 c:\temp\chromedriver.log
我得到以下信息:
[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
我对今年早些时候成功与 NSSM 包装的 chromedriver.exe 交谈有着遥远的记忆。我现在找不到代码了。我在这里错过了什么?
您正在使用 RemoteWebDriver
作为您的客户端。您提供的地址应该是 Selenium Grid hub URL,而不是 chromedriver URL.
您应该 configure Selenium Grid 了解 chromedriver,或者从 RemoteWebDriver
切换到 ChromeDriver
作为您的客户端。
ChromeDriver
连接到任何机器上 chromedriver.exe 提供的服务,这可能是关于 RemoteWebDriver
的混淆所在。RemoteWebDriver
意味着 "any browser, anywhere, as chosen by a Selenium Grid hub using criteria that you provide". ChromeDriver
表示 "any Chrome, in one specific location chosen by you".
如果切换到 ChromeDriver
,此代码运行良好:
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);
您可以为服务提供更多选项。
旁白:请注意,您将服务配置为自定义 ChromeDriver
与 chromedriver.exe 的通信方式;您配置 ChromeOptions
以自定义 Chrome 与互联网(或您的网络应用程序)对话的方式。