【问题标题】:SessionNotCreatedException: Message: session not created from disconnected: unable to connect to renderer with ChromeDriver 2.45 Chrome v71SessionNotCreatedException:消息:会话未从断开连接创建:无法使用 ChromeDriver 2.45 Chrome v71 连接到渲染器
【发布时间】:2019-05-17 10:22:18
【问题描述】:

当我使用 Python 使用 Selenium 执行此代码时:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path=r'/Users/qa/Documents/Python/chromedriver')

发生错误:

   Traceback (most recent call last):
  File "/Users/qa/Documents/Python/try.py", line 4, in <module>
    driver = webdriver.Chrome(executable_path=r'/Users/qa/Documents/Python/chromedriver')
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.44.609545 (c2f88692e98ce7233d2df7c724465ecacfe74df5),platform=Mac OS X 10.13.6 x86_64)

有人可以帮助我吗?谢谢。

【问题讨论】:

  • 使用错误跟踪日志更新问题。
  • 我正在使用 mac high sierra,我已经尝试使用也支持 Chrome v71 的 2.44 和 2.43。先生,我仍然有错误。
  • 删除 executable_path 参数时出现错误。 'chromedriver' 可执行文件需要在 PATH 中。 @DebanjanB
  • 我在寻找错误堆栈跟踪
  • @DebanjanB 对不起,先生。我已经编辑并添加了错误回溯

标签: python-3.x selenium google-chrome selenium-webdriver selenium-chromedriver


【解决方案1】:

此错误消息...

selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from disconnected: unable to connect to renderer

...暗示 ChromeDriver 无法启动/生成新的 WebBrowserChrome 浏览器 会话。

你需要考虑一个事实:

  • 当您使用 Mac OS X 时,Key executable_path 必须由 Value 支持:

    '/Users/qa/Documents/Python/chromedriver'
    
  • 所以行将是:

    driver = webdriver.Chrome(executable_path='/Users/qa/Documents/Python/chromedriver')
    

注意路径本身是一个原始路径,因此您无需添加开关r并删除它。

此外,确保您系统上的/etc/hosts 包含以下条目:

127.0.0.1 localhost.localdomain localhost
#or
127.0.0.1 localhost loopback

【讨论】:

  • 它有效,先生,我还编辑了我的主机,因为我的本地主机被阻止了。谢谢
  • 我的主机包含配置但仍然面临这个问题。我不知道该怎么做才能修复它。@DebanjanB stackoverflow.com/questions/70143701/…
【解决方案2】:

我也有类似的错误,先得到错误信息:

selenium.common.exceptions.WebDriverException:消息:未知错误:Chrome 无法启动:正常退出。 (未知错误:DevToolsActivePort 文件不存在)

通过在 ChromeOptions() 中添加 options.add_argument("--remote-debugging-port=9230") 解决了这个问题。程序运行一次,我得到了和上面一样的错误信息:

selenium.common.exceptions.SessionNotCreatedException:消息:会话未创建 从断开连接:无法连接到渲染器 (会话信息:headless chrome=89.0.4389.114)

这里的问题是 chrome 进程没有在程序中正确关闭,因此该进程在调试端口上仍然处于活动状态。要解决此问题,请关闭活动端口sudo kill -9 $(sudo lsof -t -i:9230),只需在代码末尾添加以下行:

driver.stop_client()
driver.close()
driver.quit()

由于我没有在任何地方找到此答案,我希望它对某人有所帮助。

【讨论】:

    【解决方案3】:

    在 linux 上遇到同样的问题,特别是 raspberry pi 4(所以 arm cpu)。我使用了 chromium-browser 和 chromium-chromedriver 包。

    这是我的代码:

    browser = Chrome(executable_path = '/usr/bin/chromedriver', options = opts)

    这是错误信息:

    File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
        return _run_code(code, main_globals, None,
      File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
        exec(code, run_globals)
      File "/home/ubuntu/Documents/reliant-scrape/reliant_scrape.py", line 296, in <module>
        output = logon(config['headless'], config['download'], config['site'], config['creds'])
      File "/home/ubuntu/Documents/reliant-scrape/reliant_scrape.py", line 75, in logon
        browser = Chrome(executable_path = '/usr/bin/chromedriver', options = opts)
      File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
        RemoteWebDriver.__init__(
      File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
        self.start_session(capabilities, browser_profile)
      File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
        response = self.execute(Command.NEW_SESSION, parameters)
      File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
        self.error_handler.check_response(response)
      File "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.SessionNotCreatedException: Message: session not created
    from disconnected: unable to connect to renderer
      (Session info: headless chrome=88.0.4324.150)
    

    然而,当我自己运行 chromedriver 时,它工作正常吗?我也将该行添加到/etc/hosts,但这并没有帮助。

    【讨论】:

      【解决方案4】:

      我在 Windows 10 机器上遇到了同样的问题。我必须做的是打开任务管理器并退出所有 Python.exe 进程以及所有 Chrome.exe 进程。这样做之后,

      【讨论】:

        【解决方案5】:

        如果您有 options.add_argument("--remote-debugging-port=9222") 将其更改为 options.add_argument("--remote-debugging-port=9230" )

        或者只是简单地添加 options.add_argument("--remote-debugging-port=9230") 在我的情况下修复。

        【讨论】:

        • 这为我解决了问题
        • 我不知道为什么,它对我不起作用。我正在使用 debian docker 映像。
        【解决方案6】:

        我遇到同样的错误并添加关闭代码解决问题,代码最终块如下所示:

            @staticmethod
            def fetch_music_download_url(music_name: str):
                chrome_driver_service = Service(ChromeDriverManager(chrome_type=ChromeType.GOOGLE).install())
                chrome_options = Options()
                chrome_options.add_argument("--headless")
                chrome_options.add_argument("--no-sandbox")
                chrome_options.add_argument("--disable-gpu")
                chrome_options.add_argument("--remote-debugging-port=9230")
                driver = webdriver.Chrome(service=chrome_driver_service, options=chrome_options)
                try:
                    driver.maximize_window()
                    driver.get('http://example.cn/music/?page=audioPage&type=migu&name=' + music_name)
                    driver.implicitly_wait(5)
                    // do some logic
                except Exception as e:
                    logger.error(e)
                finally:
                    // add the close logic
                    driver.stop_client()
                    driver.close()
                    driver.quit()
                    chrome_driver_service.stop()
        

        关键是你应该在使用它后通过添加chrome_driver_service.stop()关闭chrome服务。希望这段代码可以帮助其他面临同样问题的人。

        【讨论】:

          【解决方案7】:

          这对我有用 WINDOWS OS

          from selenium import webdriver
          import time
          from bs4 import BeautifulSoup
          from selenium.webdriver.chrome.options import Options
          
          options = Options()
          options.add_argument('--headless')
          options.add_argument('--disable-gpu')
          driver = webdriver.Chrome(options=options)
          driver.get(url)
          time.sleep(3)
          page = driver.page_source
          driver.quit()
          soup = BeautifulSoup(page, 'html.parser')
          
          

          希望你会发现这很有用。我也用的比较全面here

          【讨论】:

            猜你喜欢
            • 2020-06-03
            • 2019-03-25
            • 1970-01-01
            • 2021-05-05
            • 2018-05-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多