arvitaly
arvitaly

Reputation: 161

How run two Chrome driver for one profile with Selenium Webdriver Nodejs?

I write tests, and for the speed, i want, that user has already been authenticated (+ loaded data in local store).

import * as webdriver from 'selenium-webdriver';
import * as Chrome from 'selenium-webdriver/chrome';
var options = new Chrome.Options();

options.addArguments('--user-data-dir=C:\\profilepath');

var driver = new webdriver.Builder().withCapabilities(options.toCapabilities()).build();

driver.get("http://site.ru/").then(() => {
    console.log('Opened');
}, (err) => {
    console.log('Err', err);
});
var driver2 = new webdriver.Builder().withCapabilities(options.toCapabilities()).build();
driver2.get("http://site.ru/").then(() => {
    console.log('Opened');
}, (err) => {
    console.log('Error', err);
});

The first driver works good, opens the page, the second just hanging initial screen without any errors. Same for starts them in different processes ...

Upvotes: 3

Views: 5314

Answers (1)

Louis
Louis

Reputation: 151511

You have two entities working at cross-purpose here: Chrome and Selenium.

If you run Chrome from the command line and start it twice with the same profile, the second time you start it, Chrome is going to detect that there is already a Chrome instance running with the profile you selected and will instruct this Chrome instance to open a new window. I don't know what you'd see on the console on Windows but on Linux, the second time you try to start Chrome, you get this on the console:

Created new window in existing browser session.

So although it looks like you have a different Chrome instance, in fact you just have one instance with two windows. I do not believe it is possible to force Chrome to spawn a second instance with the same profile.

Then you have Selenium. When you use Builder to create a new Selenium instance, Selenium executes Chrome but as you already know from what I explained above, the 2nd time you do it, Chrome just opens a new window in the first Chrome instance you started. Selenium does not detect this but tries to connect to the browser to control it. However, Selenium already connected to the browser when you asked it to spawn the first Chrome instance, and it cannot connect again to the same instance. If you wait long enough, a timeout will occur and Selenium will report:

Error { [UnknownError: unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.13.307649 (bf55b442bb6b5c923249dd7870d6a107678bfbb6),platform=Linux 4.0.0-2-amd64 x86_64)]
  code: 13,
  state: 'unknown error',
  message: 'unknown error: Chrome failed to start: exited abnormally\n  (Driver info: chromedriver=2.13.307649 (bf55b442bb6b5c923249dd7870d6a107678bf
6_64)',
  name: 'UnknownError',
[...]

If your profile contains the credentials you need before you start your Selenium script, what you could do is just copy the profile to a new location for your second Chrome instance. It could look like this:

import * as webdriver from 'selenium-webdriver';
import * as Chrome from 'selenium-webdriver/chrome';
import * as fs_extra from 'fs-extra';

// Copy the profile to a new location for the new instance.
fs_extra.copySync("/tmp/t6/foo", "/tmp/t6/foo2");

var options = new Chrome.Options();

options.addArguments('--user-data-dir=/tmp/t6/foo');

var driver = new webdriver.Builder().withCapabilities(options.toCapabilities()).build();

driver.get("http://google.com/").then(() => {
    console.log('Opened');
}, (err) => {
    console.log('Err', err);
});

var options2 = new Chrome.Options();

options2.addArguments('--user-data-dir=/tmp/t6/foo2');

var driver2 = new webdriver.Builder().withCapabilities(options2.toCapabilities()).build();
driver2.get("http://example.com/").then(() => {
    console.log('Opened');
}, (err) => {
    console.log('Error', err);
});

Upvotes: 3

Related Questions