selenium.common.exceptions.SessionNotCreatedException:消息:未从选项卡创建的会话使用 ChromeDriver Chrome Selenium Python 崩溃

2024-04-30

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

这会重复,但不能解决我的问题:如何避免错误:selenium.common.exceptions.SessionNotCreatedException:消息:会话未从选项卡创建崩溃 https://stackoverflow.com/questions/59069926/how-to-avoid-the-error-selenium-common-exceptions-sessionnotcreatedexception-m

code:

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)

Error:

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驱动程序无法启动/产生新的浏览上下文 i.e. Chrome浏览器会议。


分析及解决方案

这个问题有多种解决方案。然而根据未知错误:由于选项卡崩溃导致页面崩溃,会话被删除 https://github.com/elgalu/docker-selenium/issues/20此问题可以通过以下任一解决方案解决:

  • 添加以下内容chrome_options:

    chrome_options.add_argument('--no-sandbox')         
    
  • 使其工作的另一个选择是添加chrome_options as --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)
    

从选项卡崩溃

从选项卡崩溃是 WIP(工作正在进行中)与铬团队相当长一段时间以来,这涉及到Linux 尝试始终使用 /dev/shm 作为不可执行内存。以下是参考资料:

  • Linux:Chrome/Chromium SIGBUS/哇,啪!在小 /dev/shm 上 https://bugs.chromium.org/p/chromium/issues/detail?id=522853
  • 当 /dev/shm 太小时,Chrome 崩溃/加载失败,并且位置无法覆盖 https://bugs.chromium.org/p/chromium/issues/detail?id=715363
  • As per 评论61#问题736452 https://bugs.chromium.org/p/chromium/issues/detail?id=736452#c61修复似乎已经完成Chrome v65.0.3299.6

参考

您可以在中找到详细的讨论未知错误:会话因未知错误导致页面崩溃而被删除:无法确定 ChromeDriver Selenium 崩溃的选项卡的加载状态 https://stackoverflow.com/questions/53902507/unknown-error-session-deleted-because-of-page-crash-from-unknown-error-cannot/53970825#53970825

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

selenium.common.exceptions.SessionNotCreatedException:消息:未从选项卡创建的会话使用 ChromeDriver Chrome Selenium Python 崩溃 的相关文章

随机推荐