设置:

  • Selenium :3.141.0
  • python :3.6.7
  • heroku-stack :heroku-18
  • headless-chrome :安装了v71.0.3578.80 buildpack
  • chromedriver :v2.44.609551 buildpack已安装

  • 在heroku中使用 Selenium 时出现此错误:
    urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
    

    我用谷歌搜索,但是没有运气。错误发生在此代码的最后一行。

    代码
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    UA = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36' \
         '(KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
    DRIVER_PATH = '/app/.chromedriver/bin/chromedriver'
    
    chrome_options = webdriver.ChromeOptions()
    chrome_options.binary_location = '/app/.apt/usr/bin/google-chrome'
    chrome_options.add_argument(f'--user-agent={UA}')
    chrome_options.add_argument(f'--proxy-server=http://my_private_proxy.com:my_port')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--no-sandbox')
    
    chrome = webdriver.Chrome(executable_path=DRIVER_PATH, options=options)
    

    最佳答案

    此错误消息...

    urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
    

    ...表示 headless 模式下的 ChromeDriver 无法启动/产生新的 Web浏览器,即 Chrome浏览器 session 。

    有关您使用的二进制文件版本的一些信息将有助于我们以更好的方式分析错误。但是,由于以下几个原因,可以忽略此问题 urllib3 :
  • 在使用非Windows操作系统时,不需要参数 --disable-gpu ,因为在讨论Headless: make --disable-gpu flag unnecessary skyos ... @ chromium.org中提到:


  • 您可以在Lost UI shared context : while initializing Chrome browser through ChromeDriver in Headless mode
  • 中找到详细的讨论。
  • 此外,您可以添加参数 --disable-dev-shm-usage 以克服有限的资源问题:
    chrome_options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
    
  • 您可以在讨论org.openqa.selenium.WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser
  • 中找到有关--disable-dev-shm-usage的详细讨论。
  • 根据urllib3.exceptions.ProtocolError: ('Connection aborted.', error(10054, 'An existing connection was forcibly closed by the remote host')),当您使用的二进制版本之间存在不兼容时,便会观察到此问题。

  • 解决方案
  • 将ChromeDriver升级到当前ChromeDriver v2.44级别。
  • 将Chrome版本保持在和Chrome v69-71 级别之间。 (as per ChromeDriver v2.44 release notes)
  • 通过IDE清理项目工作区,并仅使用必需的依赖项重建项目。
  • 如果您的基本Web客户端版本太旧,则将其卸载并安装最新的GA和Web客户端发行版本。
  • 进行系统重新引导。
  • 执行您的@Test
  • 关于python-3.x - Selenium和Heroku : urllib3.异常.ProtocolError : ('Connection aborted.' , ConnectionResetError(104, 'Connection reset by peer')),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53521883/

    10-09 01:57
    查看更多