Selenium: UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id when using driver.quit();

Created on 8 Jan 2019  路  2Comments  路  Source: SeleniumHQ/selenium

When I run this code

`

const { Builder, By } = require('selenium-webdriver')
const chrome = require("selenium-webdriver/chrome")

let chromeOptions = new chrome.Options();

chromeOptions.addArguments('disable-gpu')
chromeOptions.addArguments('headless')

class Selenium {
constructor (url, elem) {
this.url = url,
this.elem = elem
}

// finds a specfic attribute within an HTML element
async findAttr(attr) {
    let driver = new Builder().forBrowser("chrome").setChromeOptions(chromeOptions).build();

    try {
        await driver.get(this.url);
        const _elem = await driver.findElement(By.tagName(this.elem));

        return _elem.getAttribute(attr);
    } finally {
        driver.close();
    }

}

}

module.exports = Selenium

`

This error is given UnhandledPromiseRejectionWarning: NoSuchSessionError: invalid session id, I don't know why this is happening or how to fix it... I'm hoping somebody here knows a working solution.

Most helpful comment

nvm I figured out why its doing it, the browser window is simply closing too quickly.

Temp solution till it gets fixed

setTimeout(function () {driver.quit()}, 100)

All 2 comments

nvm I figured out why its doing it, the browser window is simply closing too quickly.

Temp solution till it gets fixed

setTimeout(function () {driver.quit()}, 100)

nvm I figured out why its doing it, the browser window is simply closing too quickly.

Temp solution till it gets fixed

setTimeout(function () {driver.quit()}, 100)

worked for me

Was this page helpful?
0 / 5 - 0 ratings