selenium.common.exceptions.SessionNotCreatedExcept

2020-04-21 02:56发布

问题:

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.

This make duplicate, but not solution my problem: How to avoid the error: selenium.common.exceptions.SessionNotCreatedException: Message: session not created from tab crashed

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 and driver: ChromeDriver 78.0.3904.105 (60e2d8774a8151efa6a00b1f358371b1e0e07ee2-refs/branch-heads/3904@{#877}) are compatible.

回答1:

This error message...

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

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


Analysis and Solution

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:

  • Add the following chrome_options:

    chrome_options.add_argument('--no-sandbox')         
    
  • 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 crashed

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

Reference

You can find a detailed discussion in unknown error: session deleted because of page crash from unknown error: cannot determine loading status from tab crashed with ChromeDriver Selenium