繁体   English   中英

如何让 chrome webdriver 无头运行?

[英]How to get chrome webdriver running headless?

我看过较旧的帖子,但我相信它们都使用将被弃用的代码。 下面是我正在使用的代码,它会启动浏览器,但不会在无头模式下这样做,因为有一个 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"])

能否请您指出一些有关如何使用 webdriver 的(更新的)文档或教程

这是您可以传递的服务参数。 您可以使用以下 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!

对于使用无头服务,您可以参考这个

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())

要在 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.

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