c# - C# - selenium webdriver 无法打开 chrome 浏览器窗口
问题描述
下面是在 Windows 10 Visual Studio Code 上用 C# 打开 chrome 浏览器的 selenium 脚本。我正在使用 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
解决方案
我认为您只是缺少可执行文件名称,例如
var chromeOptions = new ChromeOptions()
{
BinaryLocation = "D:\\Chrome\\chrome.exe"
};
var driver = new ChromeDriver(chromeOptions);
推荐阅读
- c++ - C++ 项目中的 WFDB 库问题(未定义引用)
- sql - 如何使用 Postgresql 复制具有 2 列但具有多个值的 csv 文件
- sql - SQL Server:如果一行的值等于目标值,则返回集合中的所有行
- c++ - 如何强制 boost program_options 严格检查选项?
- c++ - 来自 g++ -fanalyzer 的 std::vector 空指针取消引用
- python - 如何在 .env 文件中保存多个登录名以连接到 postgresql 数据库?
- python - 尽管安装了 simple-smartsheet,脚本仍返回 ModuleNotFoundError
- web-testing - 如何让 testRigor 找到这些输入?
- javascript - 不同计算机上的纪元日期转换结果不同
- r - 在用户创建的函数中对公式语法使用 quasiquotation?