How do I use Headless Chrome in Chrome 60 on Windows 10?
我一直在看以下有关无头Chrome的文章:
https://developers.google.com/web/updates/2017/04/headless-chrome
我刚刚将Windows 10上的Chrome升级到了版本60,但是当我从命令行运行以下任一命令时,似乎什么也没发生:
1 2 | chrome --headless --disable-gpu --dump-dom https://www.google.com/ chrome --headless --disable-gpu --print-to-pdf https://www.google.com/ |
我正在从以下路径(Windows上Chrome的默认安装路径)运行所有这些命令:
1 | C:\\Program Files (x86)\\Google\\Chrome\\Application\\ |
当我运行命令时,似乎需要处理一秒钟,但实际上看不到任何东西。我究竟做错了什么?
谢谢。
编辑:
如Mark Rajcok所述,如果将
因此,以下两个命令对我有用:
1 2 | "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选择器之类的PhantomJS一样逐步处理转储的DOM,但是我想这是一个单独的问题。
编辑#2:
对于它的价值,我最近遇到了一个名为Headless Chrome的Node API,名为Puppeteer(https://github.com/GoogleChrome/puppeteer),它确实易于使用,并提供了Headless Chrome的所有功能。如果您正在寻找使用Headless Chrome的简便方法,强烈建议您使用。
这对我有用:
1 | start chrome --enable-logging --headless --disable-gpu --print-to-pdf=c:\\misc\\output.pdf https://www.google.com/ |
...,但只能使用" start chrome "和"-enable-logging "并指定路径(对于pdf)-并且如果文件夹"" misc "存在于c-目录。
添加:... pdf的路径-上面的" c:\\\\\\\\ misc "-当然可以用任何其他文件夹/目录替换。
使用Chrome 61.0.3163.79,如果我添加
1 2 3 4 5 6 | >"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"> ... </body> |
如果要以编程方式控制无头Chrome,这是使用Python3和Selenium的一种方法:
在Admin cmd窗口中,安装Selenium for Python:
1 | C:\\Users\\Mark> pip install -U selenium |
下载ChromeDriver v2.32并将其解压缩。我将
1 2 3 4 5 6 7 8 | 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! |
在普通的cmd窗口中运行它:
1 2 3 4 | C:\\Users\\Mark> python headless.py <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" ... ... lots and lots of stuff here ... ...</body></html> |
当前版本(68-70)似乎需要
我使用的完整命令是:
1 2 | chrome --headless --user-data-dir=tmp --no-sandbox --enable-logging --dump-dom https://www.google.com/ > file.html chrome --headless --user-data-dir=tmp --no-sandbox --print-to-pdf=whatever.pdf https://www.google.com/ |
使用
但是,如果您尝试从HTML制作PDF,则这是无用的,因为您无法删除页眉和页脚(包含
我知道这个问题是针对Windows的,但是由于Google将该帖子作为第一个搜索结果,因此以下是在Mac上可用的方法:
Mac OS X
1 | /Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --headless --dump-dom 'http://www.google.com' |
请注意,您必须放下
更多提示
要缩进html(在real肿的真实页面中非常有用),请使用
1 | /Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --headless --dump-dom 'http://www.google.com' | tidy |
您可以通过以下方式收拾整齐:
1 | brew install tidy |
你应该很好。检查Chrome版本目录下的
1 | C:\\Program Files (x86)\\Google\\Chrome\\Application\\60.0.3112.78 |
对于命令
1 2 3 | chrome --headless --disable-gpu --print-to-pdf https://www.google.com/ C:\\Program Files (x86)\\Google\\Chrome\\Application\\60.0.3112.78\\output.pdf |
编辑:
仍然在chrome可执行文件所在的位置执行命令,在这种情况下为
1 | C:\\Program Files (x86)\\Google\\Chrome\\Application\\ |
如果您通常想避开这个问题,而只是使用某种服务来为您完成工作,那么我是无浏览器的作者/创始人,该公司致力于解决在服务中运行无头Chrome的问题-喜欢时尚。除此之外,要跟上更改并确保已安装所有适当的程序包和资源以使Chrome运行非常困难,但这绝对是可行的。