【问题标题】:--headless is not an option in chrome webdriver for selenium python--headless 不是 chrome webdriver for selenium python 中的一个选项
【发布时间】:2019-01-04 08:30:57
【问题描述】:

我想让selenium 运行一个无头的谷歌浏览器实例来从某些网站挖掘数据,而无需 UI 开销。我从here 下载了 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是否允许无头浏览?

谢谢

【问题讨论】:

  • 是的,据我所知,我永远无法在无头模式下运行 Google Chrome。我刚切换到 Firefox。
  • 我也可以在 macos 中以无头模式运行。

标签: python-3.x selenium google-chrome selenium-webdriver selenium-chromedriver


【解决方案1】:

--headless 不是chromedriver 的参数,而是Chrome 的参数。 --headless 在无头模式下运行 chrome,即没有 UI 或显示服务器依赖项。 ChromeDriver 是 WebDriver 用来控制 Chrome 的单独可执行文件,而 Webdriver 是用于驱动浏览器的特定语言绑定的集合。

我可以使用这组选项在无头模式下运行。我希望这会有所帮助:

from bs4 import BeautifulSoup, NavigableString
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import requests
import re  
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
browser = webdriver.Chrome(chrome_options=options)  # see edit for recent code change.
browser.implicitly_wait(20)

2019 年 8 月 12 日更新:

旧:browser = webdriver.Chrome(chrome_options=options)

新:browser = webdriver.Chrome(options=options)

【讨论】:

  • 但是可执行文件没有headless 选项...options.add_argument('--headless') 行是做什么的?
  • @ewwink sugeested 这是针对 chrome --headless 在无头模式下运行 chrome,即没有 UI 或显示服务器依赖项。 ChromeDriver 是 WebDriver 用来控制 Chrome 的单独的可执行文件。 Webdriver 是用于驱动浏览器的语言特定绑定的集合
  • 所以让我直截了当地说...chromedriver.exe 本身有时会调用Chrome.exe。这个想法是让它用一个额外的参数调用驱动程序?
  • 谢谢,效果很好。是的,“chrome_options”已被弃用,因此必须使用“options”。
【解决方案2】:

试试

options.headless=True

以下是我如何设置无头 chrome

options = webdriver.ChromeOptions()
options.headless=True
options.add_argument('window-size=1920x1080')
prefs = {
"download.default_directory": r"C:\FilePath\Download",
"download.prompt_for_download": False,
"download.directory_upgrade": True}
options.add_experimental_option('prefs', prefs)
chromedriver = (r"C:\Filepath\chromedriver.exe")

【讨论】:

    【解决方案3】:

    --headlesschromedriver不是 参数,而是Chrome,您可以看到更多参数或 chrome here 的命令行开关

    【讨论】:

      猜你喜欢
      • 2020-03-21
      • 2015-12-06
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多