简体   繁体   中英

How to get chrome webdriver running headless?

I've looked at older posts but I believe they all use code which will be deprecated. Below is the code I am using, this launches the browser but does not do so in headless mode as there is a GUI.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

PATH = "C:\Program Files (x86)\chromedriver.exe"
s=Service(executable_path=PATH, service_args=["--disable-gpu", "--headless"])

Could you also kindly point me to some (updated) documentation or tutorial regarding how to use the webdriver

This are the service args which you can pass. You can get with the following cmd
.\chromedriver.exe -help

Options
  --port=PORT                     port to listen on
  --adb-port=PORT                 adb server port
  --log-path=FILE                 write server log to file instead of stderr, increases log level to INFO
  --log-level=LEVEL               set log level: ALL, DEBUG, INFO, WARNING, SEVERE, OFF
  --verbose                       log verbosely (equivalent to --log-level=ALL)
  --silent                        log nothing (equivalent to --log-level=OFF)
  --append-log                    append log file instead of rewriting
  --replayable                    (experimental) log verbosely and don't truncate long strings so that the log can be replayed.
  --version                       print the version number and exit
  --url-base                      base URL path prefix for commands, e.g. wd/url
  --readable-timestamp            add readable timestamps to log
  --enable-chrome-logs            show logs from the browser (overrides other logging options)
  --allowed-ips=LIST              comma-separated allowlist of remote IP addresses which are allowed to connect to ChromeDriver
  --allowed-origins=LIST          comma-separated allowlist of request origins which are allowed to connect to ChromeDriver. Using `*` to allow any host origin is dangerous!

For using headless with services you can refer this .

import os  
from selenium import webdriver  
from selenium.webdriver.common.keys import Keys  
from selenium.webdriver.chrome.options import Options

service = webdriver.chrome.service.Service(os.path.abspath("chromedriver"))  
service.start()

chrome_options = Options()  
# chrome_options.add_argument("--headless")  
chrome_options.headless=True

# path to the binary of Chrome Canary that we installed earlier  
chrome_options.binary_location = '/Applications/Google Chrome   Canary.app/Contents/MacOS/Google Chrome Canary'

driver = webdriver.Remote(service.service_url,   desired_capabilities=chrome_options.to_capabilities())

For using headless with webdriver you can do as follows:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
webdriver.Chrome(executable_path=driver_path, options=options)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM