python-3.x - selenium.common.exceptions.SessionNotCreatedException : Message: session not created from tab crashed using ChromeDriver Chrome Selenium Python

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

我在尝试访问脚本请求的 url 时显然遇到了这个错误,但没有特定的。我不明白为什么会出现此错误,但我想对其进行处理,以免在脚本发生时中止脚本。

这会重复,但不能解决我的问题:How to avoid the error: selenium.common.exceptions.SessionNotCreatedException: Message: session not created from tab crashed

代码:

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

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--incognito')
chrome_options.add_argument('--headless')
driver = webdriver.Chrome("/driver/chromedriver", options=chrome_options)

错误:
Traceback (most recent call last):
  File "scripts/page11.py", line 15, in <module>
    driver = webdriver.Chrome(BASE_WEB_DRIVER, options=chrome_options)
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/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 tab crashed
  (Session info: headless chrome=78.0.3904.108)

Chrome :Google Chrome 78.0.3904.108和司机:ChromeDriver 78.0.3904.105 (60e2d8774a8151efa6a00b1f358371b1e0e07ee2-refs/branch-heads/3904@{#877})是兼容的。

最佳答案

此错误消息...

  selenium.common.exceptions.SessionNotCreatedException: Message: session not created
  from tab crashed
(Session info: headless chrome=78.0.3904.108)

...意味着 Chrome 驱动程序 无法启动/生成新的 浏览上下文 Chrome 浏览器 session 。

分析与解决

这个问题有多种解决方案。但是根据 UnknownError: session deleted because of page crash from tab crashed此问题可以通过以下任一解决方案来解决:
  • 添加以下 chrome_options :
    chrome_options.add_argument('--no-sandbox')         
    
  • 使其工作的另一个选项是添加 chrome_options作为 --disable-dev-shm-usage .这将强制 Chrome 使用 /tmp而是目录。这可能会减慢执行速度,因为将使用磁盘而不是内存。
    chrome_options.add_argument('--disable-dev-shm-usage')
    
  • 如此有效,您的代码块将是:
    from bs4 import BeautifulSoup
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    chrome_options = Options()
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--ignore-certificate-errors')
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-dev-shm-usage')
    driver = webdriver.Chrome(executable_path='/driver/chromedriver', options=chrome_options)
    


  • 从标签崩溃

    从标签崩溃 与 Chromium 团队合作的 WIP(正在进行的工作)已经有一段时间了,这与 Linux 试图始终将/dev/shm 用于不可执行的内存有关。以下是引用资料:
  • Linux: Chrome/Chromium SIGBUS/Aw, Snap! on small /dev/shm
  • Chrome crashes/fails to load when /dev/shm is too small, and location can't be overridden
  • 根据 Comment61#Issue 736452修复似乎已通过 登陆Chrome v65.0.3299.6


  • 引用

    您可以在 unknown error: session deleted because of page crash from unknown error: cannot determine loading status from tab crashed with ChromeDriver Selenium 中找到详细讨论。

    关于python-3.x - selenium.common.exceptions.SessionNotCreatedException : Message: session not created from tab crashed using ChromeDriver Chrome Selenium Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59186984/

    相关文章:

    python - 如何使用 Altair 在散点图中突出显示标记?

    python - 如何使用 Python 向 Twilio API 提供代理信息

    java - 基于 findElements 的列表循环是如何工作的?

    selenium - 如何通过Selenium进入Microsoft Edge上的沉浸式阅读器?

    javascript - 我正在使用 javascript 在另一个 html 页面中加载一些 html 页面。它在 Mozilla Firefox 中工作正常但在 Google Chrome 和 IE10 中不工作

    python - 列表中的运算符在打印时仍显示引号(Python 3.1)

    python-3.x - 获取 Keras 变量列表

    java - 关于 Selenium : find element by its child link text的问题

    javascript - Chrome 扩展和 javascript 提交表单

    css - 为什么谷歌浏览器不会自动中断?