如何使用 Selenium Webdriver Nodejs 为一个配置文件运行两个 Chrome 驱动程序? [英] How run two Chrome driver for one profile with Selenium Webdriver Nodejs?

查看:52
本文介绍了如何使用 Selenium Webdriver Nodejs 为一个配置文件运行两个 Chrome 驱动程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写测试,为了速度,我想要,该用户已经通过身份验证(+ 在本地存储中加载数据).

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 ...

推荐答案

这里有两个跨用途的实体:Chrome 和 Selenium.

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

如果您从命令行运行 Chrome 并使用相同的配置文件启动它两次,第二次启动它时,Chrome 将检测到已经有一个使用您选择的配置文件运行的 Chrome 实例,并会指示此 Chrome实例打开一个新窗口.我不知道你会在 Windows 的控制台上看到什么,但在 Linux 上,当你第二次尝试启动 Chrome 时,你会在控制台上看到这个:

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.

因此,尽管看起来您有一个不同的 Chrome 实例,但实际上您只有一个带有两个窗口的实例.我不认为可以强制使用 Chrome生成具有相同配置文件的第二个实例.

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.

然后你有硒.当您使用 Builder 创建新的 Selenium 实例时,Selenium 会执行 Chrome,但正如您从我上面解释的内容中已经知道的那样,第二次执行时,Chrome 只会在第一个 Chrome 实例中打开一个新窗口你开始了.Selenium 不会检测到这一点,但会尝试连接到浏览器来控制它.但是,当您要求它生成第一个 Chrome 实例时,Selenium已经连接到浏览器,并且它无法再次连接到同一个实例.如果您等待的时间足够长,一个将发生超时,Selenium 将报告:

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',
[...]

如果您的配置文件包含您在启动 Selenium 脚本之前需要的凭据,您可以做的只是将配置文件复制到您的第二个 Chrome 实例的新位置.它可能看起来像这样:

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);
});

这篇关于如何使用 Selenium Webdriver Nodejs 为一个配置文件运行两个 Chrome 驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆