简体   繁体   English

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

[英]C# - selenium webdriver unable to open chrome browser window

Below is the selenium script to open chrome browser in C# on Windows 10 Visual Studio Code. 以下是在Windows 10 Visual Studio Code上以C#打开chrome浏览器的硒脚本。 I'm using Chrome 74 (64 bit) and corresponding ChromeDriver 74.0.3729.6. 我正在使用Chrome 74(64位)和相应的ChromeDriver 74.0.3729.6。 One thing to note is Chrome is not installed in default directory. 需要注意的一件事是,默认目录中未安装Chrome。 This is due to our Office security purpose. 这是由于我们的Office安全目的。 It has installed in a different location. 它已安装在其他位置。 When I run the script It gives me error -Failed to create a Chrome process. 当我运行脚本时,它给我错误-无法创建Chrome进程。 Could some one please help me to understand why chrome browser isn't opening? 有人可以帮我了解为什么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();
        }

    }
}

Error: 错误:

    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

After adding executable name - chrome.exe 添加可执行文件名称后-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

I think your just missing the executable name, like so for example 我认为您只是缺少可执行文件名称,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有 C# 的 Selenium WebDriver 全屏打开浏览器窗口 - Open browser window in full screen using Selenium WebDriver with C# Selenium WebDriver - Chrome - C# - 无法以隐身模式启动selenium浏览器作为最大化的浏览器 - Selenium WebDriver - Chrome - C# - Unable to launch selenium browser in Incognito Mode as a maximised browser 无法在C#中使用Webdriver打开Chrome - Unable to open chrome with Webdriver in C# 如何使用 C# 在 Selenium WebDriver (Selenium 2) 中最大化浏览器窗口? - How to maximize the browser window in Selenium WebDriver (Selenium 2) using C#? 使用Selenium WebDriver C#最大化Firefox的浏览器窗口 - Maximize browser window for Firefox with Selenium WebDriver C# 无法使用 Selenium Webdriver 打开 Chrome 浏览器。 加载解压的扩展被管理员禁用 - Unable to open chrome browser using Selenium Webdriver. Loading unpacked extensions are disabled by administrator 在C#中添加Httpheader到selenium chrome webdriver - Add Httpheader to selenium chrome webdriver in C# Selenium + C#+ Chrome WebDriver:违反CSP - Selenium + C# + Chrome WebDriver: Violation of CSP 在 selenium webdriver C# 中为 chrome 设置 cookies - Setting cookies in selenium webdriver C# for chrome 在 C# 中使用 Selenium Chrome Webdriver 时出错 - Error using Selenium Chrome Webdriver with C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM