【问题标题】:Running Selenium with Headless Chrome Webdriver使用 Headless Chrome Webdriver 运行 Selenium
【发布时间】:2019-05-08 11:50:04
【问题描述】:

所以我正在尝试用 selenium 做一些事情,我真的希望它快点。

所以我的想法是使用 headless chrome 运行它会使我的脚本更快。

首先,这个假设是否正确,或者如果我使用无头驱动程序运行我的脚本无关紧要?

无论如何,我仍然想让它无头运行,但不知何故我做不到,我尝试了不同的方法,大多数人建议它会像 10 月更新中所说的那样工作

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?

但是当我尝试这样做时,我得到了奇怪的控制台输出,它似乎仍然不起作用。

感谢任何提示。

【问题讨论】:

  • 这已经过时了,或者你是什么意思?也许我错过了一点,你能澄清一下你的意思吗?
  • headless 不会让它运行得明显更快
  • @CoreyGoldberg 怎么样,你有任何消息来源吗?
  • 你应该对两者进行基准测试
  • @Rhynden 什么是奇怪的控制台输出?

标签: python selenium google-chrome selenium-chromedriver google-chrome-headless


【解决方案1】:

要运行 chrome-headless,只需通过 chrome_options.add_argument 添加 --headless,即:

from selenium import webdriver 
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#chrome_options.add_argument("--disable-extensions")
#chrome_options.add_argument("--disable-gpu")
#chrome_options.add_argument("--no-sandbox") # linux only
chrome_options.add_argument("--headless")
# chrome_options.headless = True # also works
driver = webdriver.Chrome(options=chrome_options)
start_url = "https://duckgo.com"
driver.get(start_url)
print(driver.page_source.encode("utf-8"))
# b'<!DOCTYPE html><html xmlns="http://www....
driver.quit()

所以我的想法是用无头 chrome 运行它会让我 脚本更快。

尝试使用像 --disable-extensions--disable-gpu 这样的 chrome 选项并对其进行基准测试,但我不会指望有太大的改进。


参考:headless-chrome

注意:从今天开始,在 Windows 上运行 chrome headless 时,您应该包含 --disable-gpu 标志 见crbug.com/737678

【讨论】:

  • @AndroidNoobie ukashima huksay 建议的编辑如果我没记错的话是在 2018 年 5 月实施的。它现在找到了获得代表的方法。不过,ukashima huksay 应该提到它。 (来自评论)。
  • @ukashima huksay 下次您发现此 chrome 更改时,请在更改背后的评论中提及它,就像我几周前在某个问题上所做的那样。另请参阅我之前的评论。 (来自评论)。
【解决方案2】:

如果您使用的是 Linux 环境,您可能还需要添加 --no-sandbox 以及特定的窗口大小设置。如果您正确设置用户容器,则 Windows 上不需要 --no-sandbox 标志。

仅在 Windows 上使用 --disable-gpu。其他平台不再需要它。 --disable-gpu 标志是针对一些错误的临时解决方法。

//Headless chrome browser and configure
            WebDriverManager.chromedriver().setup();
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--no-sandbox");
            chromeOptions.addArguments("--headless");
            chromeOptions.addArguments("disable-gpu");
//          chromeOptions.addArguments("window-size=1400,2100"); // Linux should be activate
            driver = new ChromeDriver(chromeOptions);

【讨论】:

    【解决方案3】:
    from time import sleep
    
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    
    driver = webdriver.Chrome(executable_path="./chromedriver", options=chrome_options)
    url = "https://stackoverflow.com/questions/53657215/running-selenium-with-headless-chrome-webdriver"
    driver.get(url)
    
    sleep(5)
    
    h1 = driver.find_element_by_xpath("//h1[@itemprop='name']").text
    print(h1)
    

    然后我在本地机器上运行脚本

    ➜ python script.py
    Running Selenium with Headless Chrome Webdriver
    

    它正在工作,并且与无头 Chrome 一起使用。

    【讨论】:

      【解决方案4】:

      Todo(在无头服务器 Debian Linux 9.4 上测试):

      1. 这样做:

        # install chrome
        curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
        echo "deb [arch=amd64]  http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
        apt-get -y update
        apt-get -y install google-chrome-stable
        
        # install chrome driver
        wget https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip
        unzip chromedriver_linux64.zip
        mv chromedriver /usr/bin/chromedriver
        chown root:root /usr/bin/chromedriver
        chmod +x /usr/bin/chromedriver
        
      2. 安装硒:

        pip install selenium
        

        并运行此 Python 代码:

        from selenium import webdriver
        from selenium.webdriver.chrome.options import Options
        options = Options()
        options.add_argument("no-sandbox")
        options.add_argument("headless")
        options.add_argument("start-maximized")
        options.add_argument("window-size=1900,1080"); 
        driver = webdriver.Chrome(chrome_options=options, executable_path="/usr/bin/chromedriver")
        driver.get("https://www.example.com")
        html = driver.page_source
        print(html)
        

      【讨论】:

        【解决方案5】:

        安装并运行容器化 Chrome:

        docker pull selenium/standalone-chrome
        docker run --rm -d -p 4444:4444 --shm-size=2g selenium/standalone-chrome
        

        使用webdriver.Remote连接:

        driver = webdriver.Remote('http://localhost:4444/wd/hub', DesiredCapabilities.CHROME)
        driver.set_window_size(1280, 1024)
        driver.get('https://www.google.com')
        

        【讨论】:

        • from selenium import webdriverdriver = webdriver.Remote('http://localhost:4444/wd/hub', webdriver.DesiredCapabilities.CHROME)
        • 这个相比 --headless 有什么优势?
        • 你从哪里得到DesiredCapabilities?我没有看到导入...我认为您的意思是使用 webdriver.DesiredCapabilities
        【解决方案6】:

        一旦你安装了 selenium 和 web 驱动程序。下面为我​​在 Linux 集群上使用无头 Chrome 工作:

        from selenium import webdriver
        options = webdriver.ChromeOptions()
        options.add_argument("--headless")
        options.add_argument("--disable-extensions")
        options.add_argument("--disable-dev-shm-usage")
        options.add_argument("--no-sandbox")
        options.add_experimental_option("prefs",{"download.default_directory":"/databricks/driver"})
        driver = webdriver.Chrome(chrome_options=options)
        

        【讨论】:

          猜你喜欢
          • 2018-06-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-12
          • 2018-01-19
          • 2016-12-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多