在macos 平台上使用jenkins 时出现此错误消息...
WebDriverError: no such session
(Driver info: chromedriver=a.b.c (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.11.5 x86_64)
或者
linux 平台上的此错误消息...
WebDriverError: no such session
(Driver info: chromedriver=p.q.r,platform=Linux 3.2.0-4-amd64 x86_64) (Selenium::WebDriver::Error::NoSuchDriverError)
或者
windows 平台上的此错误消息...
WebDriverError: no such session
(Driver info: chromedriver=x.y.z (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Windows NT 6.1 SP1 x86_64) (NoSuchDriver)
...暗示 ChromeDriver 无法与现有的 Browsing Context 即 Chrome 浏览器 会话进行通信。
我们已经在Issue 732: No such session error - inconsistent problem which appears when running tests for a prolonged period 的讨论中详细讨论了这个问题。此错误通常在长时间执行 Test Suite 后出现,如下所示:
[489.798][DEBUG]: DEVTOOLS EVENT Inspector.targetCrashed {
}
[489.798][INFO]: Waiting for pending navigations...
[489.798][INFO]: Done waiting for pending navigations
[0127/105308:ERROR:nacl_helper_linux.cc(289)] NaCl helper process running without a sandbox!
Most likely you need to configure your SUID sandbox correctly
[489.849][INFO]: RESPONSE FindElements unknown error: session deleted because of page crash
from tab crashed
(Session info: chrome=p.q.r.s)
[489.849][DEBUG]: Log type 'driver' lost 0 entries on destruction
[489.849][DEBUG]: Log type 'browser' lost 9 entries on destruction
这个错误在nacl_helper_linux.cc中定义如下:
// If the Zygote has started handling requests, we should be sandboxed via
// the setuid sandbox.
if (!IsSandboxed()) {
LOG(ERROR) << "NaCl helper process running without a sandbox!\n"
<< "Most likely you need to configure your SUID sandbox "
<< "correctly";
正是由于沙盒问题,FindElement(s) 方法FAILED,而Page Crash 发生是由于session deletion
解决方案
此错误可能由于多种原因而发生,解决此错误的解决方案如下:
- 使用参数
--disable-impl-side-painting 启动Chrome 会话配置ChromeDriver
-
当服务器无法识别唯一会话标识符时也会出现此错误。如果会话已被删除或会话 ID 在以下任一情况下无效,则会发生这种情况:
-
Explicit session deletion:显式调用quit() 方法时会显式删除WebDriver 会话,如下所示:
from selenium import webdriver
from selenium.common.exceptions import InvalidSessionIdException
driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
print("Current session is {}".format(driver.session_id))
driver.quit()
try:
driver.get("https://www.google.com/")
except Exception as e:
print(e.message)
#Console Output:
Current session is a9272550-c4e5-450f-883d-553d337eed48
No active session with ID a9272550-c4e5-450f-883d-553d337eed48
-
Implicit session deletion:当您关闭最后一个调用close() 方法的窗口或选项卡时,WebDriver 会话将被隐式删除,如下所示:
driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
print("Current session is {}".format(driver.session_id))
# closes current window/tab
driver.close()
try:
driver.get("https://www.google.com/")
except Exception as e:
print(e.message)
#Console Output:
Current session is a9272550-c4e5-450f-883d-553d337eed48
No active session with ID a9272550-c4e5-450f-883d-553d337eed48
您可能还需要添加参数 --no-sandbox
- 由于
/dev/shm 太小,Chrome 似乎在某些页面上的 Docker 容器中经常崩溃。同样,您可能需要修复较小的 /dev/shm 大小。
-
一个例子:
sudo mount -t tmpfs -o rw,nosuid,nodev,noexec,relatime,size=512M tmpfs /dev/shm
如果您使用-v /dev/shm:/dev/shm 选项共享它也可以使用主机 /dev/shm
-
另一种方法是将chrome_options 添加为--disable-dev-shm-usage。这将强制 Chrome 使用 /tmp 目录。这可能会减慢执行速度,因为将使用磁盘而不是内存。
chrome_options.add_argument('--disable-dev-shm-usage')
从标签崩溃
from tab crashed 是 Chromium 团队 的 WIP(Work In Progress) 已经有一段时间了,这与 Linux 有关试图始终将 /dev/shm 用于不可执行的内存。以下是参考:
参考
您可以在以下位置找到一些详细的讨论: