Python 如何配置 ChromeDriver 以通过 Selenium 在 Headless 模式下启动 Chrome 浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46920243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
提问by Maz
I'm working on a python script to web-scrape and have gone down the path of using Chromedriver as one of the packages. I would like this to operate in the background without any pop-up windows. I'm using the option 'headless' on chromedriver and it seems to do the job in terms of not showing the browser window, however, I still see the .exe file running. See the screenshot of what I'm talking about. Screenshot
我正在编写一个用于网络抓取的 python 脚本,并且已经走上了使用 Chromedriver 作为软件包之一的道路。我希望它在没有任何弹出窗口的情况下在后台运行。我在 chromedriver 上使用了“headless”选项,它似乎在不显示浏览器窗口方面完成了这项工作,但是,我仍然看到 .exe 文件正在运行。请参阅我正在谈论的屏幕截图。截屏
This is the code I am using to initiate ChromeDriver:
这是我用来启动 ChromeDriver 的代码:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument('headless')
options.add_argument('window-size=0x0')
chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"
Things I've tried to do is alter the window size in the options to 0x0 but I'm not sure that did anything as the .exe file still popped up.
我试图做的事情是将选项中的窗口大小更改为 0x0,但我不确定是否做了任何事情,因为 .exe 文件仍然弹出。
Any ideas of how I can do this?
关于我如何做到这一点的任何想法?
I am using Python 2.7 FYI
我正在使用 Python 2.7 仅供参考
采纳答案by Maz
So after correcting my code to:
所以在更正我的代码后:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"])
options.add_argument('--disable-gpu')
options.add_argument('--headless')
chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe"
The .exe file still came up when running the script. Although this did get rid of some extra output telling me "Failed to launch GPU process".
运行脚本时 .exe 文件仍然出现。尽管这确实消除了一些告诉我“无法启动 GPU 进程”的额外输出。
What ended up working is running my Python script using a .bat file
最终的工作是使用 .bat 文件运行我的 Python 脚本
So basically,
所以基本上,
- Save python script if a folder
Open text editor, and dump the following code (edit to your script of course)
c:\python27\python.exe c:\SampleFolder\ThisIsMyScript.py %*
Save the .txt file and change the extension to .bat
- Double click this to run the file
- 如果是文件夹,则保存python脚本
打开文本编辑器,并转储以下代码(当然编辑到您的脚本)
c:\python27\python.exe c:\SampleFolder\ThisIsMyScript.py %*
保存 .txt 文件并将扩展名更改为 .bat
- 双击它运行文件
So this just opened the script in Command Prompt and ChromeDriver seems to be operating within this window without popping out to the front of my screen and thus solving the problem.
所以这只是在命令提示符中打开了脚本,ChromeDriver 似乎在这个窗口中运行,而没有弹出到我的屏幕前面,从而解决了问题。
回答by Daniel Porteous
It should look like this:
它应该是这样的:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu') # Last I checked this was necessary.
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
This works for me using Python 3.6, I'm sure it'll work for 2.7 too.
这对我使用 Python 3.6 有效,我相信它也适用于 2.7。
Update 2018-10-26: These days you can just do this:
2018-10-26 更新:这些天你可以这样做:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
回答by DebanjanB
While working with Selenium Client 3.11.x, ChromeDriver v2.38and Google Chrome v65.0.3325.181in Headlessmode you have to consider the following points :
在Headless模式下使用Selenium Client 3.11.x、ChromeDriver v2.38和Google Chrome v65.0.3325.181 时,您必须考虑以下几点:
- You need to add the argument
--headless
to invoke Chrome in headless mode. - For Windows OS systemsyou need to add the argument
--disable-gpu
- As per Headless: make --disable-gpu flag unnecessary
--disable-gpu
flag is not required on Linux Systemsand MacOS. - As per SwiftShader fails an assert on Windows in headless mode
--disable-gpu
flag will become unnecessary on Windows Systemstoo. - Argument
start-maximized
is required for a maximized Viewport.- Here is the link to details about Viewport.
You may require to add the argument
--no-sandbox
to bypass the OS security model.- Here is the link to the Sandboxstory.
Sample Windows Code block :
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("--headless") # Runs Chrome in headless mode. options.add_argument('--no-sandbox') # Bypass OS security model options.add_argument('--disable-gpu') # applicable to windows os only options.add_argument('start-maximized') # options.add_argument('disable-infobars') options.add_argument("--disable-extensions") driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe') driver.get("http://google.com/") print ("Headless Chrome Initialized on Windows OS")
Sample Linux Code block :
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("--headless") # Runs Chrome in headless mode. options.add_argument('--no-sandbox') # # Bypass OS security model options.add_argument('start-maximized') options.add_argument('disable-infobars') options.add_argument("--disable-extensions") driver = webdriver.Chrome(chrome_options=options, executable_path='/path/to/chromedriver') driver.get("http://google.com/") print ("Headless Chrome Initialized on Linux OS")
- 您需要添加参数
--headless
以在无头模式下调用 Chrome。 - 对于Windows 操作系统系统,您需要添加参数
--disable-gpu
- 根据Headless:
--disable-gpu
在Linux 系统和MacOS上不需要 make --disable-gpu 标志不必要的标志。 - 根据SwiftShader 在无头模式下在 Windows 上的断言,
--disable-gpu
标志在Windows 系统上也将变得不必要。 start-maximized
最大化Viewport需要参数。- 这是有关Viewport详细信息的链接。
您可能需要添加参数
--no-sandbox
以绕过操作系统安全模型。- 这是沙盒故事的链接。
示例 Windows 代码块:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("--headless") # Runs Chrome in headless mode. options.add_argument('--no-sandbox') # Bypass OS security model options.add_argument('--disable-gpu') # applicable to windows os only options.add_argument('start-maximized') # options.add_argument('disable-infobars') options.add_argument("--disable-extensions") driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe') driver.get("http://google.com/") print ("Headless Chrome Initialized on Windows OS")
示例 Linux 代码块:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("--headless") # Runs Chrome in headless mode. options.add_argument('--no-sandbox') # # Bypass OS security model options.add_argument('start-maximized') options.add_argument('disable-infobars') options.add_argument("--disable-extensions") driver = webdriver.Chrome(chrome_options=options, executable_path='/path/to/chromedriver') driver.get("http://google.com/") print ("Headless Chrome Initialized on Linux OS")
Update (23-April-2018)
更新(2018 年 4 月 23 日)
Invoking Google Chrome Browserin Headless Modeprogramatically have become much easier with the availability of the method set_headless(headless=True)
as follows :
随着该方法的可用性,以编程方式在Headless 模式下调用Google Chrome 浏览器变得更加容易,如下所示:set_headless(headless=True)
Documentation :
set_headless(headless=True) Sets the headless argument Args: headless: boolean value indicating to set the headless option
Sample Code :
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.set_headless(headless=True) driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe') driver.get("http://google.com/") print ("Headless Chrome Initialized") driver.quit()
文件:
set_headless(headless=True) Sets the headless argument Args: headless: boolean value indicating to set the headless option
示例代码:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.set_headless(headless=True) driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe') driver.get("http://google.com/") print ("Headless Chrome Initialized") driver.quit()
Note:
--disable-gpu
argument is implemented internally.
注意:
--disable-gpu
参数是在内部实现的。
Update (13-October-2018)
更新(2018 年 10 月 13 日)
To invoke Chrome Browser in headless mode now you can just set the --headless
property through Options()
class as follows:
现在要以无头模式调用 Chrome 浏览器,您只需--headless
通过Options()
类设置属性,如下所示:
Sample Code :
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.headless = True driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe') driver.get("http://google.com/") print ("Headless Chrome Initialized") driver.quit()
示例代码:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.headless = True driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe') driver.get("http://google.com/") print ("Headless Chrome Initialized") driver.quit()
Outro
奥特罗
How to make firefox headless programmatically in Selenium with python?
如何使用python在Selenium中以编程方式使firefox无头?
tl; dr
tl; 博士
Here is the link to the Sandboxstory.
这是沙盒故事的链接。
回答by Aurel Branzeanu
The .exe would be running anyway. According to Google - "Run in headless mode, i.e., without a UI or display server dependencies."
Better prepend 2 dashes to command line arguments, i.e.
options.add_argument('--headless')
In headless mode, it is also suggested to disable the GPU, i.e.
options.add_argument('--disable-gpu')
.exe 无论如何都会运行。根据谷歌 - “以无头模式运行,即没有 UI 或显示服务器依赖项。”
最好在命令行参数前加上 2 个破折号,即
options.add_argument('--headless')
在headless模式下,也建议禁用GPU,即
options.add_argument('--disable-gpu')
回答by Sudheer Muhammed
Try using ChromeDriverManager
尝试使用 ChromeDriverManager
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.set_headless()
browser =webdriver.Chrome(ChromeDriverManager().install(),chrome_options=chrome_options)
browser.get('https://google.com')
# capture the screen
browser.get_screenshot_as_file("capture.png")