如何在无头模式下启动 ChromeDriver [英] How to start ChromeDriver in headless mode

查看:48
本文介绍了如何在无头模式下启动 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆