我想让 Selenium 运行一个无头的 Google Chrome 实例,以在没有 UI 开销的情况下从某些网站挖掘数据。我从 这里 下载了 ChromeDriver 可执行文件并将其复制到我当前的脚本目录中。
该驱动程序似乎可以与 Selenium 一起正常工作并且能够自动浏览,但是我似乎找不到无头选项。大多数将 Selenium 与 headless Chrome 结合使用的在线示例大致如下:
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'`
driver = webdriver.Chrome(executable_path=os.path.abspath(“chromedriver"), chrome_options=chrome_options)
driver.get("http://www.duo.com")`
但是,当我使用命令 chromedriver -h
检查 Selenium WebDriver 的可能参数时,这就是我得到的:
D:\Jobs\scripts>chromedriver -h
Usage: chromedriver [OPTIONS]
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
--whitelisted-ips comma-separated whitelist of remote IP addresses which are allowed to connect to ChromeDriver
没有 --headless
选项可用。
从上面的链接获取的 ChromeDriver 是否允许无头浏览?
原文由 user32882 发布,翻译遵循 CC BY-SA 4.0 许可协议
--headless
不是chromedriver
的参数,而是Chrome
的参数。--headless
以无头模式运行 chrome,即没有 UI 或显示服务器依赖项。 ChromeDriver 是 WebDriver 用来控制 Chrome 的独立可执行文件,而 Webdriver 是驱动浏览器的特定语言绑定的集合。我可以使用这组选项以无头模式运行。我希望这个能帮上忙:
2019 年 8 月 12 日更新:
旧:
browser = webdriver.Chrome(chrome_options=options)
新:
browser = webdriver.Chrome(options=options)