如何在无头模式下启动 ChromeDriver

How to start ChromeDriver in headless mode(如何在无头模式下启动 ChromeDriver)
本文介绍了如何在无头模式下启动 ChromeDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想试试 headless chrome,但我遇到了这个问题,我无法在 headless 模式下启动驱动程序.我正在关注 google 文档.我错过了什么吗?代码执行卡在 var browser = new ChromeDriver();

I want to try out headless chrome, but I am running into this issue, that I can't start the driver in headless mode. I was following google documentation. am I missing something ? The code execution gets stuck in var browser = new ChromeDriver(); line

这是我的代码:

var chromeOptions = new ChromeOptions
{
    BinaryLocation = @"C:Users2-as AukstasDocumentsVisual Studio 2017ProjectsChromeTestChromeTestinDebugchromedriver.exe",
    DebuggerAddress = "localhost:9222"
};

chromeOptions.AddArguments(new List<string>() {"headless", "disable-gpu" });

var browser = new ChromeDriver(chromeOptions);


browser.Navigate().GoToUrl("https://stackoverflow.com/");
Console.WriteLine(browser.FindElement(By.CssSelector("#h-top-questions")).Text);

推荐答案

更新
Chrome 60 版已经发布,所以您只需通过 Nuget 下载 Chromdriver 和 Selenium 并使用这个简单的代码,一切都会像魅力一样运行.太棒了.

UPDATE
Chrome version 60 is out so all you need to do is to download Chromdriver and Selenium via Nuget and use this simple code and everything works like a charm. Amazing.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

...



var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");

using (var browser = new ChromeDriver(chromeOptions))
{
  // add your code here
}

日期

在 Chrome 60 正式版发布之前,有一个解决方案.您可以下载 Chrome Canary 并使用 headless.安装后将 BinaryLocation 设置为指向 chrome canary 还注释掉 DebuggerAddress 行(它强制 chrome 超时):

There is a solution until the official release of Chrome 60 will be released. You can download Chrome Canary and use headless with it. After installation set BinaryLocation to point to chrome canary also comment out the DebuggerAddress line(it forces chrome to timeout):

var chromeOptions = new ChromeOptions
{
    BinaryLocation = @"C:Users2-as AukstasAppDataLocalGoogleChrome SxSApplicationchrome.exe",
    //DebuggerAddress = "127.0.0.1:9222"
};

chromeOptions.AddArguments(new List<string>() { "no-sandbox", "headless", "disable-gpu" });

var _driver = new ChromeDriver(chromeOptions);

这篇关于如何在无头模式下启动 ChromeDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!


本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)