【问题标题】:Remote Webdriver Chrome throws a "path to the driver executable" error远程 Webdriver Chrome 抛出“驱动程序可执行文件的路径”错误
【发布时间】:2026-01-30 18:45:01
【问题描述】:

您好,当我使用以下代码时

IWebDriver _webDriver = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"),
                DesiredCapabilities.Chrome());

我得到以下错误

System.InvalidOperationException :驱动程序可执行文件的路径必须由 webdriver.chrome.driver 系统属性设置;有关详细信息,请参阅http://code.google.com/p/selenium/wiki/ChromeDriver。最新版本可从http://code.google.com/p/chromedriver/downloads/list下载 TearDown : System.NullReferenceException : 对象引用未设置为对象的实例。 在 OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(响应错误响应) 在 OpenQA.Selenium.Remote.RemoteWebDriver.Execute(字符串 driverCommandToExecute,Dictionary`2 参数) 在 OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities) 在 Browser.cs 中的 Testframework.Browser.RemoteGoto(String browser, String url):第 86 行 在 CommonAction.cs 中的 Testframework.CommonAction.RemoteBrowser(String browser):第 70 行 在RegistrationTest.cs中的Test.RegistrationTest.InvalidRegistrationTest(字符串浏览器,字符串用户名,字符串密码,字符串确认密码,字符串安全问题,字符串安全答案,字符串错误消息文本,字符串名字,字符串姓氏):第50行 - 拆除 在 CommonAction.cs 中的 Testframework.CommonAction.CaptureScreen(String fileName):第 121 行 在 RegistrationTest.cs 中的 Test.RegistrationTest.SnapshotOnFailure():第 590 行

【问题讨论】:

    标签: c# webdriver remotewebdriver


    【解决方案1】:

    线索确实在错误中。

    Chrome 应该安装在运行测试或被指向的系统上。

    退一步,看看文档:

    https://code.google.com/p/selenium/wiki/ChromeDriver

    另外,如果 Chrome 安装在一个特殊的位置,您需要将 Selenium 指向它的位置。同样,这在文档中进行了解释。

    在 C# 中:

    DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
    capabilities.SetCapability("chrome.binary", this.binaryLocation);
    

    或:

    ChromeOptions options = new ChromeOptions();
    options.BinaryLocation = "pathtogooglechrome";
    capabilities.SetCapability(ChromeOptions.Capability, options);
    

    【讨论】:

    • 嗨,我只能找到 java 示例,你能告诉我如何在 C# webdriver remote 中设置 chrome 的路径,因为我在 C#q 中找不到等价物
    • 谢谢你,这是我设置的路径 options.BinaryLocation = @"..\RequiredFiles\chromedriver_win_26.0.1383.0\chromedriver.exe";我收到以下错误“驱动程序可执行文件的路径必须由 webdriver.chrome.driver 系统属性设置”
    • 试过这个 System.Environment.SetEnvironmentVariable("webdriver.chrome.driver",@"/path/to/where/you/ve/put/chromedriver.exe") C#等效的java系统。 setProperty("webdriver.chrome.driver","/path/to/where/you/ve/put/chromedriver.exe");没有用
    • 如果您直接使用 ChromeDriver(查看文档),它会启动 Chrome 吗?
    • 是的,当我执行此“ChromeDriver(ChromeDriverpath)”时它可以工作,但我无法在远程 webdriver 中设置 chromedriverpath。我不知道为什么
    【解决方案2】:

    除了更改代码之外,您还可以采用其他方式。
    下载 chrome 驱动并设置 PATH 环境变量指向 chromedriver.exe 所在的目录。

    重新启动您的 IDE/命令控制台并运行测试。好用!!!

    【讨论】:

    • 谢谢你除了将其设置为环境变量之外还有其他方法吗?我还发现这个解决方案在启动控制台时设置了 chromedriver 路径(java -Dwebdriver.chrome.driver=C:\example\chromedriver.exe -jar selenium-server-standalone-2.31.0.jar)。我想尽可能避免这种解决方案并在代码中设置路径,以便代码保持可移植性:)
    • @AnandS 为了保持代码的可移植性,最好不要硬编码驱动程序的位置。我更喜欢设置路径,因为开发的测试可以移植到任何机器上,只需设置 PATH 即可使用
    • 谢谢你,我很想知道为什么当我使用这个 System.Environment.SetEnvironmentVariable("webdriver.chrome.driver",@"/path/to/w‌​here/you/ve /put/chromedriver.exe") chromedriver.exe 没有设置环境变量