python-3.x - 如何运行 chrome headless 浏览器

标签 python-3.x selenium selenium-chromedriver headless-browser google-chrome-headless

我一直在尝试在我的 mac 上设置 chrome headless 浏览器,但出现错误。

我尝试按照这些教程进行引用:

https://intoli.com/blog/running-selenium-with-headless-chrome/ https://duo.com/decipher/driving-headless-chrome-with-python

并使用这些 stackoflow 页面

How to get Chromedriver in headless mode to run headless?selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with chrome

headless 浏览器在 phantomjs 上工作,但我知道 selenium 不再希望我们使用它。这就是我现在正在运行的:(几乎完全是堆栈溢出的响应之一)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://www.duo.com")
print("Chrome Browser Initialized in Headless Mode")

这是我的回复:

Traceback (most recent call last):
  File "headless_browser.py", line 47, in <module>
    driver = webdriver.Chrome(chrome_options=chrome_options)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
    desired_capabilities=desired_capabilities)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/Users/BCohen/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.36.540469 (1881fd7f8641508feb5166b7cae561d87723cfa8),platform=Mac OS X 10.13.2 x86_64)

最佳答案

错误确实给了我们如下提示:

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.36.540469 (1881fd7f8641508feb5166b7cae561d87723cfa8),platform=Mac OS X 10.13.2 x86_64)

错误表明 ChromeDriver 没有在预期位置找到 Chrome 二进制文件。

可以有以下两种解决方案:

  • 根据 Requirements Chrome 安装需要在一个明确的位置。

  • 作为替代方案,您可以通过 Options 类的实例传递 Chrome 二进制文件的绝对路径,如下所示:

    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    
  • 最后,在启动 WebDriverWebClient 时,您需要发送参数 Key executable_path使用 chrome_path

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.binary_location = "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
    options = Options()
    options.add_argument("--headless")
    options.add_argument("--no-sandbox")
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    chrome_path = "/home/ec2-user/chrome/chromedriver"
    driver = webdriver.Chrome(executable_path=chrome_path, chrome_options=options)
    driver.get("http://www.duo.com")
    print("Chrome Browser Initialized in Headless Mode")
    

备选

Headless 模式 中以编程方式调用Google Chrome 浏览器 随着方法的可用性 set_headless(headless=True) 变得更加容易如下:

  • 文档:

    set_headless(headless=True)
        Sets the headless argument
    
        Args:
            headless: boolean value indicating to set the headless option
    
  • 示例代码:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.set_headless(headless=True)
    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:\path\to\chromedriver.exe')
    driver.get("http://google.com/")
    print ("Headless Chrome Initialized")
    driver.quit()
    

关于python-3.x - 如何运行 chrome headless 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49581940/

相关文章:

python - 如何绘制单个 3D 点?

selenium - 尝试通过网络路径访问 chromedriver 时,CreatePlatformSocket() 返回错误 : An invalid argument was supplied. (0x2726)

python - 在 Python 中使用 Selenium 抓取随时间变化的动态 URL

python-3.x - 如何使用 Python 在 Chrome 浏览器中打开 Google Sheet Doc?

python - Selenium - 如何知道下一页是否不存在?

python - 类型错误 : 'Popen' object is not callable

python-3.x - 断言错误 : Some objects had attributes which were not restored

python-3.x - 如何修复构建神经网络时出现的以下错误?

python - 在 Selenium 中提取隐藏元素

java - ChromeDriver 需要安装 Chrome 吗?