System.InvalidOperationException 未处理 HResult=-2146233079 Message=无法创建新服务:ChromeDriverService

System.InvalidOperationException was unhandled HResult=-2146233079 Message=Unable to create new service: ChromeDriverService

我正在尝试使用 Selenium Grid 和 C# 来远程控制 Chrome 浏览器。它曾经可以工作,但自从我在我的机器 B 上安装了一个新的 Windows 图像后,它就不再工作了。但是,我可以在两台计算机上本地使用 Selenium,所以我认为这不是 chromedriver 的问题。远程控制,我在A机和B机分别运行如下命令:

java -jar C:\Users\FlcUser\Downloads\selenium-server-standalone-3.141.5.jar -role hub -host 10.1.1.14

java -Dwebdrive.chrome.drive="C:\Temp\chromedriver.exe" -jar C:\Temp\selenium-server-standalone-3.141.5.jar -role node -hub http://10.1.1.14:4444/grid/register

节点注册成功,然后我运行这段C#代码(不知道我评论的是否有必要,但改不了错误):

ChromeOptions options = new ChromeOptions();
        //options.BinaryLocation = "C:\Temp\chromedriver.exe";
        IWebDriver driver = new RemoteWebDriver(new Uri("http://10.1.1.14:4444/wd/hub"), options.ToCapabilities(), TimeSpan.FromSeconds(180));

我收到这个错误:

Unable to create new service: ChromeDriverService

我读了很多关于它的资料,也尝试了很多东西。我更新了一切。我已确保我的 chromedriver 版本与我的浏览器兼容,并且我还有最新版本的 Java(https://java.com/en/download/,在两台机器上)。

这些是我在 cmd 中得到的日志:

机器A:

17:48:23.746 INFO [DefaultGridRegistry.add] - Registered a node http://10.24.25.237:19016
17:50:19.814 INFO [RequestHandler.process] - Got a request to create a new session: Capabilities {browserName: chrome, goog:chromeOptions: {}}
17:50:35.155 INFO [TestSlot.getNewSession] - Trying to create a new session on test slot {server:CONFIG_UUID=2dac93b3-8952-4c62-bcee-3c5febe21545, seleniumProtocol=WebDriver, browserName=chrome, maxInstances=5, platformName=WIN10, platform=WIN10}

机器 B:

21:48:31.497 INFO [ActiveSessionFactory.apply] - Capabilities are: {
"browserName": "chrome",
"goog:chromeOptions": {}}
21:48:31.497 INFO [ActiveSessionFactory.lambda$apply] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)

这是 Visual Studio 抛出的异常:

System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=Unable to create new service: ChromeDriverService
Build info: version: '3.141.5', revision: 'd54ebd709a', time: '2018-11-06T11:58:47'
System info: host: 'FLCOMPACT', ip: '10.1.1.25', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_191'
Driver info: driver.version: unknown (SessionNotCreated)
  Source=WebDriver
  StackTrace:
       at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
       at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
       at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
       at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
       at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout)
       at SeleniumPrueba.Program.Main(String[] args) in d:\SeleniumDemo\SeleniumPrueba\Program.cs:line 21
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

最后,我在其他电脑上也试过同样的错误,所以我做错了。我认为它与 Java 或 chromedriver 有关,因为我曾经 运行 这个 VS Code 解决方案没有问题。我还通过 NuGet 更新了 Visual Studio 中的库。

我该如何解决这个问题?

问题似乎出在您用来启动 Selenium 网格节点 的命令中,而不是:

java -Dwebdrive.chrome.drive="C:\Temp\chromedriver.exe" -jar C:\Temp\selenium-server-standalone-3.141.5.jar -role node -hub http://10.1.1.14:4444/grid/register

应该是:

java -Dwebdriver.chrome.driver=C:/Temp/chromedriver.exe -jar C:/Temp/selenium-server-standalone-3.141.5.jar -role node -hub http://10.1.1.14:4444/grid/register

注释 AKeywebdriver.chrome.driver 而不是 webdrive.chrome.drive

注释 B:确保 ChromeDriver 二进制文件位于 Grid NodeC:/Temp/