这些脚本是不同的脚本,同样的脚本不会再次出现“no such session”错误。
我有时会遇到这种情况。我使用的是 Laravel Dusk 的 ChromeDriver,而不是 Selenium。不过,我认为原因在于 ChromeDriver,而不是 Selenium。
ChromeDriver 会在文件夹 C:\Users\(yourAccountName)\AppData\Local\Temp
中创建一些缓存文件。在此文件夹中,你会看到许多看起来像 scoped_dir1234_5678
的缓存文件夹。每个文件夹占用约 10mb 的空间。如果 Jenkins 经常运行 ChromeDriver,ChromeDriver 可能会过度填充临时文件夹中的缓存文件夹。你应该考虑在 C 驱动器上有 30-50GB 的缓存文件,并充分利用 C 驱动器。
当我的 C 驱动器没有空间时,ChromeDriver 将无法启动,然后返回错误消息 "Facebook\WebDriver\Exception\NoSuchDriverException: no such session"。
解决方案:
--更新--
找到另一种导致此问题的情况。
如果你在同一台操作系统上同时运行相同的脚本来启动两个不同的 ChromeDriver 实例,当一个实例结束并关闭 chromedriver 时,另一个 chrome 浏览器实例也可能被关闭。
例如,你打开两个控制台并执行 chromeDriver 脚本,或者你的 Jenkins 项目同时启动。
我认为即使你运行不同的脚本,但在同一时间需要 chromeDriver,其中一个脚本也会由于 chrome 浏览器实例的关闭而出现 "no such session" 的情况。
解决方案:
我的情况是使用 Laravel Dusk 而没有使用 Selenium。我不确定当测试通过 Selenium 服务器进行时是否会有所不同。
open $TMPDIR
命令找到临时文件夹(https://apple.stackexchange.com/questions/94964/where-is-the-temp-folder)。然后我发现 VS Code 生成了一个巨大的 TypeScript 文件夹。一旦清理干净,我的 Spectron 测试就可以再次运行了。 - cham在使用jenkins时,在macos平台上出现了以下错误信息...
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)
[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
// 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)
方法由于沙盒问题而失败,页面崩溃是由于会话删除导致的。
此错误可能出现的原因有很多,以下是解决此错误的方法:
--disable-impl-side-painting
--enable-gpu-rasterization
which allow heuristics to determine when a layer tile should be drawn with the Skia GPU backend. Only valid with GPU accelerated compositing + impl-side painting.--force-gpu-rasterization
which always uses the Skia GPU backend for drawing layer tiles. Only valid with GPU accelerated compositing + impl-side painting. Overrides the kEnableGpuRasterization
flag.This error is also observed when the server does not recognize the unique session identifier. This happens if the session has been deleted or if the session ID is invalid in either of the following ways:
Explicit session deletion: A WebDriver session is explicitly deleted when explicitly invoking the quit()
method as follows:
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: A WebDriver session is implicitly deleted when you close the last window or tab invoking close()
method as follows:
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
You may also have to add the argument --no-sandbox
/dev/shm
. Similarly, you may have to fix the small /dev/shm
size.An example:
sudo mount -t tmpfs -o rw,nosuid,nodev,noexec,relatime,size=512M tmpfs /dev/shm
It also works if you use -v /dev/shm:/dev/shm
option to share host /dev/shm
Another way 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')
页面崩溃是与Chromium团队合作的正在进行中的工作,与Linux试图始终使用/dev/shm来存储非可执行内存有关。以下是参考资料:
您可以在以下链接中找到一些详细讨论:
我遇到的情况是在错误的位置调用了driver.quit()。
nosuchsessionException
检查您的代码,可能是您调用了 Driver.close();
或 Driver.quit();
两次或同时。
解决方案:删除其中一个。
我在使用chromediver_binary
和selenium
进行Python Selenium端到端测试时遇到了这个问题。错误是由于尝试多次运行driver.close()
引起的。
我意识到我的方法被多次调用,而我真正想要的是setUpClass
和tearDownClass
。我将放置我的最终解决方案,因为它避免了这个错误,并且对我的目的非常有效。
class SeleniumTestCase(TestCase):
"""
A wrapper of TestCase which will launch a selenium server, login, and add
cookies in the setUp phase of each test.
"""
@classmethod
def setUpClass(cls, *args, **kwargs):
cls.driver = webdriver.Chrome(port=4444)
cls.driver.implicitly_wait(15)
cls.driver.maximize_window()
cls.driver.get(HOST)
# page obect I wrote which accepts the driver and can login to my app
cls.login = LoginPage(cls.driver)
cls.login.log_into_app()
@classmethod
def tearDownClass(cls):
cls.driver.close()
这使我能够编写像这样的测试:
class TestNavigation(SeleniumTestCase):
def setUp(self):
# Initialize page objects for each test
self.module1 = Module1Page(self.driver)
self.module2 = Module2Page(self.driver)
# launch page
self.driver.get(HOST)
def test_module1(self):
self.module1.nav_link.click()
self.assertEqual(self.module1.title.text, 'Module One')
def test_module2(self):
self.module2.nav_link.click()
self.assertEqual(self.module2.title.text, 'Module Two')
在我最初的登录中,我使用SeleniumTestCase
,它会持续存在于我运行的所有测试中,因此我可以编写测试方法来针对单个功能进行测试,就像我习惯做的那样。
-v /dev/shm:/dev/shm
来运行容器(或在docker-compose文件中设置一些大小)。无论如何,这仍然可能是一个间歇性的错误,就像在Chromium(上一个评论)中所说的那样。 - Tom