selenium.common.exceptions.SessionNotCreatedException:消息:未从选项卡创建的会话使用 ChromeDriver Chrome Selenium Python 崩溃 [英] selenium.common.exceptions.SessionNotCreatedException: Message: session not created from tab crashed using ChromeDriver Chrome Selenium Python

查看:19
本文介绍了selenium.common.exceptions.SessionNotCreatedException:消息:未从选项卡创建的会话使用 ChromeDriver Chrome Selenium Python 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having this error apparently when trying to access a url that the script requests, does not have a specific. I don't understand exactly why this error, but I want to treat it so as not to abort the script when it occurs.

这会重复,但不能解决我的问题:如何避免错误:selenium.common.exceptions.SessionNotCreatedException: Message: session not created from tab crashed

This make duplicate, but not solution my problem: 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}) 兼容.

Chrome: Google Chrome 78.0.3904.108 and driver: ChromeDriver 78.0.3904.105 (60e2d8774a8151efa6a00b1f358371b1e0e07ee2-refs/branch-heads/3904@{#877}) are compatible.

推荐答案

这个错误信息...

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

...暗示 ChromeDriver 无法启动/生成新的浏览上下文,即 Chrome 浏览器 会话.

...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.

这个问题有多种解决方案.但是根据 UnknownError: session deleted because page crashed from tab crashed 这个问题可以可通过以下任一方案解决:

There are diverse solution to this issue. However as per UnknownError: session deleted because of page crash from tab crashed this issue can be solved by either of the following solutions:

  • 添加以下chrome_options:

chrome_options.add_argument('--no-sandbox')         

  • 另一个使它起作用的选项是将 chrome_options 添加为 --disable-dev-shm-usage.这将强制 Chrome 使用 /tmp 目录.这可能会减慢执行速度,因为将使用磁盘而不是内存.

  • Another option to make it work would be to add the chrome_options as --disable-dev-shm-usage. This will force Chrome to use the /tmp directory instead. This may slow down the execution though since disk will be used instead of memory.

    chrome_options.add_argument('--disable-dev-shm-usage')
    

  • 如此有效,您的代码块将是:

  • So effectively, you code block will be:

    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)
    

  • from tab crashedChromium 团队 的 WIP(Work In Progress) 已经有一段时间了,这与 Linux 有关试图始终将/dev/shm 用于不可执行的内存.以下是参考:

    from tab crashed was WIP(Work In Progress) with the Chromium Team for quite some time now which relates to Linux attempting to always use /dev/shm for non-executable memory. Here are the references :

    • 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
    • As per Comment61#Issue 736452 the fix seems to be have landed with Chrome v65.0.3299.6

    您可以在 未知错误:会话因未知错误导致页面崩溃而被删除:无法从 ChromeDriver Selenium 崩溃的选项卡中确定加载状态

    这篇关于selenium.common.exceptions.SessionNotCreatedException:消息:未从选项卡创建的会话使用 ChromeDriver Chrome Selenium Python 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆