C#-Selenium WebDriver无法打开Chrome浏览器窗口

时间:2019-07-18 19:48:09

标签: c# selenium-webdriver selenium-chromedriver

下面是在Windows 10 Visual Studio Code中以C#打开chrome浏览器的硒脚本。我正在使用Chrome 74(64位)和相应的ChromeDriver 74.0.3729.6。需要注意的一件事是,默认目录中未安装Chrome。这是由于我们的Office安全目的。它已安装在其他位置。当我运行脚本时,它给我错误-无法创建Chrome进程。有人可以帮我了解为什么Chrome浏览器无法打开吗?

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
using SeleniumExtras.PageObjects;

namespace NAS
{
    [TestFixture]
    class Program
    {
        public static IWebDriver driver;

        public Program(){}

        [OneTimeSetUp]    
        public void startBrowser()
        {

            ChromeOptions opt = new ChromeOptions();
            opt.BinaryLocation = @"C:\\ProgramData\\Microsoft\\AppV\\Client\\Integration\\520B8677-106E-430B-8927-6F9261C56329\\Root\\VFS\\ProgramFilesX86\\Google\\Chrome\\Application\\";      
            driver = new ChromeDriver(@"C:\\Progra~1\\Selenium",opt);

            driver.Navigate().GoToUrl("http://www.google.com");            
            driver.Manage().Window.Maximize();
        }

        [Test]
        public void testLogin()
        {
            Console.WriteLine("test running..");
        }

        [OneTimeTearDown]
        public void closeBrowser()
        {
            driver.Close();
        }

    }
}

错误:

    Running selected tests in c:\NAS\bin\Debug\netcoreapp2.2\NAS.dll
   NUnit3TestExecutor converted 1 of 1 NUnit test cases
TearDown failed for test fixture NAS.Program
**OpenQA.Selenium.WebDriverException : unknown error: Failed to create a Chrome process.**
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.14393 x86_64)
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
   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.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
   at NAS.Program.startBrowser() in c:\NAS\Program.cs:line 30
--TearDown
   at NAS.Program.closeBrowser() in c:\NAS\Program.cs:line 46
NUnit Adapter 3.12.0.0: Test execution complete
NAS.Program.testLogin: failed

Total tests: 1. Passed: 0. Failed: 1. Skipped: 0

添加可执行文件名称后-chrome.exe

Running selected tests in c:\NAS\bin\Debug\netcoreapp2.2\NAS.dll
   NUnit3TestExecutor converted 1 of 1 NUnit test cases
SetUp failed for test fixture NAS.Program
OpenQA.Selenium.WebDriverException : unknown error: Chrome failed to start: crashed
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location C:\ProgramData\Microsoft\AppV\Client\Integration\520B8677-106E-430B-8927-6F9261C56329\Root\VFS\ProgramFilesX86\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.14393 x86_64)
   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.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
   at NAS.Program.startBrowser() in c:\NAS\Program.cs:line 28
NUnit Adapter 3.12.0.0: Test execution complete
NAS.Program.testLogin: failed

Total tests: 1. Passed: 0. Failed: 1. Skipped: 0

1 个答案:

答案 0 :(得分:0)

我认为您只是缺少可执行文件名称,例如

    var chromeOptions = new ChromeOptions()
    {
        BinaryLocation = "D:\\Chrome\\chrome.exe"
    };
    var driver = new ChromeDriver(chromeOptions);