简体   繁体   English

如何让 chrome webdriver 无头运行?

[英]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.下面是我正在使用的代码,它会启动浏览器,但不会在无头模式下这样做,因为有一个 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能否请您指出一些有关如何使用 webdriver 的(更新的)文档或教程

This are the service args which you can pass.这是您可以传递的服务参数。 You can get with the following cmd您可以使用以下 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:要在 webdriver 中使用 headless,您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Headless Chrome Webdriver 运行 Selenium - Running Selenium with Headless Chrome Webdriver 如何使用 selenium webdriver 在无头 chrome 上允许通知 - How to allow notifications on headless chrome using selenium webdriver 如何在 python 中使用 chrome webdriver headless with.pac 文件代理? - how to use chrome webdriver headless with .pac file proxy in python? 是否可以防止无头 Chrome 窗口在运行时启动? - Is it possible to prevent the headless Chrome window get up when running it? Selenium Chrome Webdriver 无法在带有配置文件的无头模式下工作 - Selenium Chrome Webdriver not working in headless mode with profile 使用无头 chrome webdriver 时出现超时异常错误 - timeout exception error on using headless chrome webdriver 从python运行无头Chrome - Running headless Chrome from python Python Selenium Chrome无法无头运行 - Python Selenium Chrome Not Running Headless 使用 Headless 什么都不会发生,没有 Headless 它可以完美地收集 webdriver.chrome 和 webdriver.firefox 中的数据 - With Headless nothing happens and without Headless it collects the data perfectly in both webdriver.chrome and webdriver.firefox Chrome 在没有无头模式的情况下无法运行(Ubuntu 18.04) - Chrome not running without headless mode (Ubuntu 18.04)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM