代码之家  ›  专栏  ›  技术社区  ›  user32882

--在Selenium python的chrome webdriver中,headless不是一个选项

  •  0
  • user32882  · 技术社区  · 5 年前

    我想要 selenium 运行google chrome的无头实例,从某些网站挖掘数据,而不需要用户界面开销。我从下载了Chromedriver可执行文件 here 并将其复制到我当前的脚本目录。这个驱动程序看起来和Selenium配合得很好,可以自动浏览,但是我似乎找不到Headless选项。大多数在线使用示例 有了无头铬合金,就有了:

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

    但是,当我使用命令检查Selenium WebDriver的可能参数时 chromedriver -h 这就是我得到的:

    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是否允许无头浏览?

    谢谢

    2 回复  |  直到 5 年前
        1
  •  2
  •   ShivaGaire    5 年前

    --headless 不是的参数 chromedriver 但为了 Chrome . --无头的 在headless模式下运行chrome,即没有用户界面或显示服务器依赖项。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)
    browser.implicitly_wait(20)
    
        2
  •  1
  •   ewwink    5 年前

    --headless 论证 chromedriver 但是 Chrome ,您可以看到更多的参数或用于chrome的命令行开关。 here