node.jsmacosselenium-webdriverfirefoxgeckodriver

How to solve 'NoSuchSessionError' when setting up Selenium with Firefox?


I'm new to Selenium (and automated testing in general) and am trying to set up using Selenium with Node.js, using Firefox (104.0) as the browser (on MacOS 12.4).

Following several tutorials online I have:

  1. Set up a new npm project

  2. Installed the Selenium Webdriver npm package

  3. Downloaded geckodriver.exe from https://github.com/mozilla/geckodriver/releases

  4. Added new PATH variable for geckodriver

  5. Set up the following JS test script:

    var webdriver = require('selenium-webdriver');

    var driver = new webdriver.Builder(). withCapabilities(webdriver.Capabilities.firefox()). build();

    driver.get('http://www.lambdatest.com'); driver.quit();

Running this script opens Firefox for a moment, but then produces the error:

Selenium Manager binary found at /Users/me/Desktop/automated-testing/node_modules/selenium-webdriver/bin/macos/selenium-manager
Driver path: /Users/me/.cache/selenium/geckodriver/mac-arm64/0.33.0/geckodriver
Browser path: /Applications/Firefox.app/Contents/MacOS/firefox
/Users/me/Desktop/automated-testing/node_modules/selenium-webdriver/lib/error.js:524
    let err = new ctor(data.message)
              ^

NoSuchSessionError: Tried to run command without establishing a connection
    at Object.throwDecodedError (/Users/me/Desktop/automated-testing/node_modules/selenium-webdriver/lib/error.js:524:15)
    at parseHttpResponse (/Users/me/Desktop/automated-testing/node_modules/selenium-webdriver/lib/http.js:601:13)
    at Executor.execute (/Users/me/Desktop/automated-testing/node_modules/selenium-webdriver/lib/http.js:529:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async thenableWebDriverProxy.execute (/Users/me/Desktop/automated-testing/node_modules/selenium-webdriver/lib/webdriver.js:745:17) {
  remoteStacktrace: ''
}

I've tried clearing caches and moving the geckodriver file around, to no effect. I've not been able to find anything else related to this error in relation to Selenium.

Would anyone know what I should be doing?


Solution

  • Turns out this was working correctly. In my JS script I had the line:

    driver.quit();

    without any conditions around it, which of course just shut down the browser as soon as it opened.