问题
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
This is the code I am using to initiate 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.
Any ideas of how I can do this?
I am using Python 2.7 FYI
回答1:
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.
Update 2018-10-26: These days you can just do this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
回答2:
While working with Selenium Client 3.11.x, ChromeDriver v2.38 and Google Chrome v65.0.3325.181 in Headless mode you have to consider the following points :
- You need to add the argument
--headless
to invoke Chrome in headless mode. - For Windows OS systems you need to add the argument
--disable-gpu
- As per Headless: make --disable-gpu flag unnecessary
--disable-gpu
flag is not required on Linux Systems and MacOS. - As per SwiftShader fails an assert on Windows in headless mode
--disable-gpu
flag will become unnecessary on Windows Systems too. - 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 Sandbox story.
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")
Update (23-April-2018)
Invoking Google Chrome Browser in Headless Mode programatically have become much easier with the availability of the method set_headless(headless=True) as follows :
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()
Note :
--disable-gpu
argument is implemented internally.
Update (13-October-2018)
To invoke Chrome Browser in headless mode now you can just set the --headless
property through Options()
class as follows:
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()
Outro
How to make firefox headless programmatically in Selenium with python?
tl; dr
Here is the link to the Sandbox story.
回答3:
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')
回答4:
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".
What ended up working is running my Python script using a .bat file
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
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.
来源:https://stackoverflow.com/questions/46920243/how-to-configure-chromedriver-to-initiate-chrome-browser-in-headless-mode-throug