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

查看:401
本文介绍了如何以无头模式启动ChromeDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试无头镀铬,但是遇到了这个问题,我无法以无头模式启动驱动程序。我正在关注 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:\Users\2-as Aukstas\Documents\Visual Studio 2017\Projects\ChromeTest\ChromeTest\bin\Debug\chromedriver.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并使用它。安装后,将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:\Users\2-as Aukstas\AppData\Local\Google\Chrome SxS\Application\chrome.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天全站免登陆