简体   繁体   English

--headless 不是 selenium python chrome webdriver 中的一个选项

[英]--headless is not an option in chrome webdriver for selenium python

I would like to have selenium run a headless instance of google chrome to mine data from certain websites without the UI overhead.我想让selenium运行一个无头的谷歌浏览器实例来从某些网站挖掘数据,而没有 UI 开销。 I downloaded the chromedriver executable from here and copied it to my current scripting directory.我从 这里下载了 chromedriver 可执行文件并将其复制到我当前的脚本目录。 The driver appears to work fine with selenium and is able to browse automatically, however I cannot seem to find the headless option.该驱动程序似乎与 selenium 一起工作正常并且能够自动浏览,但是我似乎找不到无头选项。 Most online examples of using selenium with headless chrome go something along the lines of:大多数将selenium与无头铬一起使用的在线示例都遵循以下原则:

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")` 

However when I inspect the possible arguments for the selenium webdriver using the command chromedriver -h this is what I get:但是,当我使用命令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

No --headless option is available.没有--headless选项可用。

Could anyone provide some clarification for this?任何人都可以为此提供一些说明吗? Does the chromedriver obtained from the link above allow for headless browsing?从上面的链接获得的 chromedriver 是否允许无头浏览?

Thanks谢谢

--headless is not argument for chromedriver but for Chrome . --headless不是争论chromedriver但对于Chrome --headless Run chrome in headless mode, ie, without a UI or display server dependencies. --headless在无头模式下运行 chrome,即没有 UI 或显示服务器依赖项。 ChromeDriver is a separate executable that WebDriver uses to control Chrome and Webdriver is aa collection of language specific bindings to drive a browser. ChromeDriver 是一个单独的可执行文件,WebDriver 使用它来控制 Chrome,而 Webdriver 是一组特定于语言的绑定来驱动浏览器。

I am able to run in headless mode with this set of options.我可以使用这组选项在无头模式下运行。 I hope this will help:我希望这个能帮上忙:

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)

Update 12 Aug 2019: 2019 年 8 月 12 日更新:

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

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

Try尝试

options.headless=True options.headless=真

The following is how I set up my headless chrome以下是我如何设置我的无头 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")

--headless不是chromedriver而是Chrome参数,你可以在这里看到更多的参数或 chrome 的命令行开关

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

相关问题 Webdriver Manager+Chrome Headless+Selenium+Python:webdriver不响应选项 - Webdriver Manager+Chrome Headless+Selenium+Python: webdriver does not respond to options Windows Python Selenium Webdriver 只能在无头 Chrome 中运行,而不能在带有 UI 的 Chrome 中运行 - Windows Python Selenium Webdriver can only run in headless Chrome and not in Chrome with UI 无法使用 Python3 禁用 Selenium Chrome WebDriver 在无头模式下下载图像 - Unable to disable downloading images in headless mode for Selenium Chrome WebDriver using Python3 Python Selenium Webdriver:如何避免无头 Chrome 并在 Windows 10 上的新窗口中运行 - Python Selenium Webdriver: How to avoid headless Chrome and run in new window on Windows 10 instead Selenium、Python 和 Chrome Webdriver 问题 - Selenium , Python and Chrome Webdriver problem Linux上使用Python的Xvfb Selenium无头Chrome - Xvfb Selenium headless Chrome on Linux in Python Selenium 和无头 chrome 通过 python 下载 - Selenium and headless chrome downloads via python 如何检查 Python Selenium Chrome headless 是否正常工作? - How to check if Python Selenium Chrome headless is working? 如何在 Python 中使用无头模式和 selenium webdriver 节省数据抓取时间 - How to save time in scraping datas with headless mode and selenium webdriver in Python 在 chrome webdriver python selenium 上禁用控制台消息 - disable console messages on chrome webdriver python selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM