如何在Windows 10的Chrome 60中使用无头版Chrome? [英] How do I use Headless Chrome in Chrome 60 on Windows 10?

查看:2739
本文介绍了如何在Windows 10的Chrome 60中使用无头版Chrome?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读关于无头Chrome的以下文章:

https://developers.google.com/web/updates/2017/04/headless-chrome



我刚刚升级Chrome从Windows 10到版本60,但是当我从命令行运行以下任一命令时,似乎都没有发生:

  chrome --headless --disable-gpu --dump-dom https://www.google.com/ 
chrome --headless --disable-gpu --print-to-pdf https:// www。 google.com/b $ b

我从以下路径运行所有这些命令(默认安装在Windows上使用Chrome的路径):

  C:\程序文件(x86)\Google\Chrome\Application\\ \\ 

当我运行这些命令时,有些东西似乎处理了一秒钟,但实际上我没有看到什么。我做错了什么?

谢谢。






编辑:

正如Mark Rajcok所述,如果您将 - enable-logging 添加到 --dump-dom 命令,它工作。此外,在Chrome 61.0.3163.79中, - print-to-pdf 命令也可以使用,但您可能必须按顺序指定输出文件的不同路径以获得必要的权限来保存它。



因此,以下两条命令对我有效:

 C:\ Program Files(x86)\Google\Chrome\Application\chrome--headless --disable-gpu --enable-logging --dump-dom https://www.google.com/ 
C:\ Program Files(x86)\Google\Chrome\Application\chrome--headless --disable-gpu --print-to -pdf = D:\output.pdf https://www.google.com/

我猜测接下来的步骤是能够通过DOM选择器和whatnot来完成像PhantomJS一样的转储DOM,但我想这是一个单独的问题。 解决方案

使用Chrome 61.0.3163.79,如果我添加 - enable-logging ,那么 - dump-dom 产生输出:

 > C:\ Program Files(x86)\Google\Chrome\Application\chrome.exe--enable-logging --headless --disable-gpu --dump-dom https://www.chromestatus .com 
< body class =loadingdata-path =/ features>
< app-drawer-layout fullbleed =>
...
< / script>
< / body>






如果您想以编程方式控制无头Chrome,一种使用Python3和Selenium的方法:



在Admin cmd窗口中,安装Selenium for Python:

  C:\Users\Mark> pip install -U selenium 

下载 ChromeDriver v2.32并解压缩。我把 chromedriver.exe 放在 C:\ Users \ Mark 中,这就是我把这个 headless.py Python脚本:


$ b

  from selenium import webdriver 
$ b $ options = webdriver.ChromeOptions()
options.add_argument(headless)#如果你想看到浏览器的弹出窗口,请删除这一行
driver = webdriver.Chrome (chrome_options = options)
driver.get('https://www.google.com/')
print(driver.page_source)
driver.quit()#不要错过这个,或者chromedriver.exe会继续运行!

在普通的cmd窗口中运行它:

  C:\Users\Mark> python headless.py 
<!DOCTYPE html>< html xmlns =http://www.w3.org/1999/xhtml...
...很多很多东西...
...< / body>< / html>


I've been looking at the following article about Headless Chrome:
https://developers.google.com/web/updates/2017/04/headless-chrome

I just upgraded Chrome on Windows 10 to version 60, but when I run either of the following commands from the command line, nothing seems to happen:

chrome --headless --disable-gpu --dump-dom https://www.google.com/
chrome --headless --disable-gpu --print-to-pdf https://www.google.com/

And I'm running all of these commands from the following path (the default installation path for Chrome on Windows):

C:\Program Files (x86)\Google\Chrome\Application\

When I run the commands, something seems to process for a second, but I don't actually see anything. What am I doing wrong?
Thanks.


Edit:

As noted by Mark Rajcok, if you add --enable-logging to the --dump-dom command, it works. Also, the --print-to-pdf command works as well in Chrome 61.0.3163.79, but you'll probably have to specify a different path for the output file in order to have the necessary permissions to save it.

As such, the following two commands worked for me:

"C:\Program Files (x86)\Google\Chrome\Application\chrome" --headless --disable-gpu --enable-logging --dump-dom https://www.google.com/
"C:\Program Files (x86)\Google\Chrome\Application\chrome" --headless --disable-gpu --print-to-pdf=D:\output.pdf https://www.google.com/

I guess the next step is being able to step through the dumped DOM like PhantomJS with DOM selectors and whatnot, but I suppose that's a separate question.

解决方案

With Chrome 61.0.3163.79, if I add --enable-logging then --dump-dom produces output:

> "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --enable-logging --headless --disable-gpu --dump-dom https://www.chromestatus.com
<body class="loading" data-path="/features">
<app-drawer-layout fullbleed="">
...
</script>
</body>


If you want to programatically control headless Chrome, here's one way to do it with Python3 and Selenium:

In an Admin cmd window, install Selenium for Python:

C:\Users\Mark> pip install -U selenium

Download ChromeDriver v2.32 and extract it. I put the chromedriver.exe in C:\Users\Mark, which is where I put this headless.py Python script:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("headless")  # remove this line if you want to see the browser popup
driver = webdriver.Chrome(chrome_options = options)
driver.get('https://www.google.com/')
print(driver.page_source)
driver.quit()  # don't miss this, or chromedriver.exe will keep running!

Run it in a normal cmd window:

C:\Users\Mark> python headless.py
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" ...
...  lots and lots of stuff here ...
...</body></html>

这篇关于如何在Windows 10的Chrome 60中使用无头版Chrome?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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