Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - Interact with exsting browser session using selenium-webdriver library in NodeJS

http://tarunlalwani.com/post/reusing-existing-browser-session-selenium/

This post provides the insight for using existing session for selenium, but this is in Python/Java. Wanted to implement the same logic in NodeJS using selenium-webdriver library.

I am able to access the session id using:

    driver.getSession().then( function(session) {
         console.log('Session:'+session.getId());
    });

But how to get the executor value?

CHecked and found webdriver.WebDriver.attachToSession method will attach to the existing session, but need the value for executor and session for the same.

like image 346
stackjohnny Avatar asked Oct 18 '25 16:10

stackjohnny


1 Answers

It is possible to attach to existing webdriver session with Node

You just need to create WebDriver object manually, without builder.

const _http = require('selenium-webdriver/http');

//todo: replace with your session ID and selenium url
let sessionId = 'cddab287623789c665c1dbc5c64bf702';
let url = 'http://localhost:4444/wd/hub';

let driver = new WebDriver(
    sessionId,
    new _http.Executor(Promise.resolve(url)
        .then(
            url => new _http.HttpClient(url, null, null))
    )
);

More complex example: test will try to use existing session, and if it does not work - will create a new one.

const {Builder, By, Key, until, WebDriver} = require('selenium-webdriver');
const _http = require('selenium-webdriver/http');

(async function example() {
    //todo: replace this value with session ID after test is executed first time
    let sessionId = 'cddab287623789c665c1dbc5c64bf702';
    let url = 'http://localhost:4444/wd/hub';
    let browser = 'chrome';
    let startUrl = 'http://www.google.com/ncr';

    //Connect to existing session
    let driver = await new WebDriver(
        sessionId,
        new _http.Executor(Promise.resolve(url)
            .then(
                url => new _http.HttpClient(url, null, null))
        )
    );

    //Trying to open URL. If does not work - we need to re-create a session
    await driver.get(startUrl).catch(async r => {
        console.log('Session "' + sessionId + '" not found. Creating new session.');
        driver = await new Builder()
            .usingServer(url)
            .forBrowser(browser)
            .build();
        driver.getSession().then(function(e){
            console.log('Session: ' + JSON.stringify(e, null, 2));
        });
        driver.get(startUrl);
    });

    console.log('Starting test execution');

    try {
        await driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN);
        await driver.wait(until.titleIs('webdriver - Google Search'), 1000);
    } finally {
        //todo: We intentionally do not close the session in order to use it next time
        // await driver.quit();
    }
})();
like image 51
Maksym Moskvychev Avatar answered Oct 20 '25 04:10

Maksym Moskvychev