Alcorta Project
Alcorta Project

Reputation: 11

Selenium - Driver.quit() produces an "UnhandledPromiseRejectionWarning: NoSuchSessionError" after closing webdriver

I made an API with express in Node. That API has an endpoint, which when called, it executes a method that starts a selenium webdriver instance and fills a form in a webpage. At the end of that method, I try to execute driver.quit() to close the webdriver, but no matter what, I get the same error:

(node:18608) UnhandledPromiseRejectionWarning: NoSuchSessionError: Tried to run command without establishing a connection
    at Object.throwDecodedError (D:\bot\node_modules\selenium-webdriver\lib\error.js:517:15)
    at parseHttpResponse (D:\bot\node_modules\selenium-webdriver\lib\http.js:655:13)
    at Executor.execute (D:\bot\node_modules\selenium-webdriver\lib\http.js:581:28)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async thenableWebDriverProxy.execute (D:\bot\node_modules\selenium-webdriver\lib\webdriver.js:724:17)
    at async thenableWebDriverProxy.findElements (D:\bot\node_modules\selenium-webdriver\lib\webdriver.js:1020:17)
(Use `node --trace-warnings ...` to show where the warning was created)

Environment

What I did

As per the selenium documentation, I'm using await/async to handle all the promises of the webdriver. Furthermore, I surrounded the execution of the method with a try-catch. Here's a stripped-down version of the main method:

let driver;    

module.exports.bot = async function(user, password, var_a, var_b) {
    try {
        await (driver = new Builder().forBrowser('firefox').setFirefoxOptions(options).build());

        logger("Starting...");
        // Go to main page
        await driver.get("https://www.example.com");
        // Find the dropdown and hover it
        await hoverDropdown();

        const button = await driver.findElement(By.id("j_idt8:j_idt18"));
        await button.click();

        await login(user, password);
        logger("Logged in successfuly");
        await fillForm(var_a, var_b);    

        logger("Finishing...");
    } catch (error) {
        throw error;
    } finally {
        if (driver)
            await driver.quit();           
    }
};

After that method finishes executing (event after driver.quit is executed), node crashes with the UnhandledPromiseRejectionWarning.

What should I do? Could somebody lend me a hand?

Upvotes: 0

Views: 734

Answers (1)

Alcorta Project
Alcorta Project

Reputation: 11

I've discovered the error. I'm going to post it in case someone stumble upons the same (or a similar) issue.

Turns out, I was wrongly using the "wait" method of selenium in my code. The documentation says that "the wait will repeatedly evaluate the condition until it returns a truthy value".

As my syntax was wrong, the wait method never timed out, so it kept looping... even after executing driver.quit();

That's why the error that crashed Node had this line. It was trying to find the element.

at async thenableWebDriverProxy.findElements (D:\bot\node_modules\selenium-webdriver\lib\webdriver.js:1020:17)

Incorrect syntax:

await driver.wait(until.elementLocated(By.id("msgError"), 1000));

Correct syntax:

await driver.wait(until.elementLocated(By.id("msgError")), 1000);

It's also important to notice that this method throws an exception (timeout exception), so you must try-catch it too.

Upvotes: 1

Related Questions