WebDriver错误:在使用ChromeDriver Chrome通过Jenkins和Selenium时没有这样的会话错误。

19
当我从Jenkins运行脚本时,经常会出现“no such session”错误。这是什么原因?是连接失败还是因为其他原因(我正在运行26个脚本之一至少没有这种错误)。
这些脚本是不同的脚本,同样的脚本不会再次出现“no such session”错误。

你能提供更多信息吗?你使用的是哪个浏览器和Selenium版本?你是否在运行Selenium Grid?如果是,你设置了多少个节点? - Tom
我正在使用最新版本的Selenium,配合Chrome和ChromeDriver,但问题仍然存在。 - Shan
你可以在Chromium的讨论页面上查看长时间的讨论。我也仍然感到头痛。我正在使用Selenium3和Docker镜像,但无法获得稳定的行为。 - Tom
我进行了检查,但我不认为这是内存问题,因为我正在16GB的机器上运行这些程序。 - Shan
就像我之前所说的那样,你的问题缺少详细信息。如果你正在使用Docker运行Selenium Grid(你只是说你从Jenkins运行,但这个信息在你的问题或评论中并没有提供),就如Selenium Github页面所述,你应该使用 -v /dev/shm:/dev/shm 来运行容器(或在docker-compose文件中设置一些大小)。无论如何,这仍然可能是一个间歇性的错误,就像在Chromium(上一个评论)中所说的那样。 - Tom
我在问题描述中已经说过了。我在我的Jenkins节点上运行测试用例,但不确定为什么会时不时出现这种情况。我对Docker不太了解,需要学习并尝试在其中运行。 - Shan
5个回答

10

我有时会遇到这种情况。我使用的是 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"。

解决方案:

  1. 进入临时文件夹,删除所有 ChromeDriver 缓存文件夹可清理 C 空间。
  2. 创建脚本以删除/清理 ChromeDriver 的缓存文件夹。

--更新--

找到另一种导致此问题的情况。

如果你在同一台操作系统上同时运行相同的脚本来启动两个不同的 ChromeDriver 实例,当一个实例结束并关闭 chromedriver 时,另一个 chrome 浏览器实例也可能被关闭。

例如,你打开两个控制台并执行 chromeDriver 脚本,或者你的 Jenkins 项目同时启动。

我认为即使你运行不同的脚本,但在同一时间需要 chromeDriver,其中一个脚本也会由于 chrome 浏览器实例的关闭而出现 "no such session" 的情况。

解决方案:

  1. 在 Jenkins 中安装构建阻塞插件
  2. 设置项目在构建阻塞器中,目标项目需要等待它完成。

我的情况是使用 Laravel Dusk 而没有使用 Selenium。我不确定当测试通过 Selenium 服务器进行时是否会有所不同。


你是对的!在运行每个测试用例之前不要关闭 Chrome 实例。只需在 after 块中关闭它,这样每个测试用例都可以使用相同的 Chrome 实例而没有任何错误。 - Balasubramani M
类似这样的方法对我也起作用了,但在 Mac 上,你需要使用 open $TMPDIR 命令找到临时文件夹(https://apple.stackexchange.com/questions/94964/where-is-the-temp-folder)。然后我发现 VS Code 生成了一个巨大的 TypeScript 文件夹。一旦清理干净,我的 Spectron 测试就可以再次运行了。 - cham

4

在使用时,在平台上出现了以下错误信息...

WebDriverError: no such session
  (Driver info: chromedriver=a.b.c (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.11.5 x86_64)

或者

平台上的这个错误信息...

WebDriverError: no such session
    (Driver info: chromedriver=p.q.r,platform=Linux 3.2.0-4-amd64 x86_64) (Selenium::WebDriver::Error::NoSuchDriverError)

或者

平台上出现的这个错误信息...

WebDriverError: no such session 
(Driver info: chromedriver=x.y.z (52179c1b310fec1797c81ea9a20326839860b7d3),platform=Windows NT 6.1 SP1 x86_64) (NoSuchDriver)

这意味着ChromeDriver无法与现有的浏览上下文,即Chrome浏览器会话进行通信。
我们在讨论732号问题:长时间运行测试时出现的“无此会话错误-不一致问题”中详细讨论了这个问题。这个错误通常在执行测试套件一段时间后观察到:
[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) 方法由于沙盒问题而失败页面崩溃是由于会话删除导致的。

解决方案

此错误可能出现的原因有很多,以下是解决此错误的方法:

  • Initiate the Chrome session configuring ChromeDriver with the argument --disable-impl-side-painting
    • Additionally, you can also add the argument --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.
    • As an option, you can also add the argument --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

  • Chrome seem to crash often in Docker containers on certain pages due to too small /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来存储非可执行内存有关。以下是参考资料:


参考资料

您可以在以下链接中找到一些详细讨论:


3

我遇到的情况是在错误的位置调用了driver.quit()。


0

nosuchsessionException 检查您的代码,可能是您调用了 Driver.close();Driver.quit(); 两次或同时。

解决方案:删除其中一个。


0

我在使用chromediver_binaryselenium进行Python Selenium端到端测试时遇到了这个问题。错误是由于尝试多次运行driver.close()引起的。

我意识到我的方法被多次调用,而我真正想要的是setUpClasstearDownClass。我将放置我的最终解决方案,因为它避免了这个错误,并且对我的目的非常有效。

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,它会持续存在于我运行的所有测试中,因此我可以编写测试方法来针对单个功能进行测试,就像我习惯做的那样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接