当尝试访问脚本请求的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})
兼容。
答案 0 :(得分:1)
此错误消息...
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from tab crashed
(Session info: headless chrome=78.0.3904.108)
...表示 ChromeDriver 无法启动/产生新的浏览上下文,即 Chrome浏览器会话。
对此问题有多种解决方案。但是,根据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( Work In Progress ),现在已经有一段时间了,这与 Linux有关尝试始终将/ dev / shm用于不可执行的内存。这里是参考: