Возможно присоединение к существующему сеансу веб-драйвера с узлом
Вам просто нужно создать объект WebDriver вручную, без компоновщика.
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))
)
);
Более сложный пример: test попытается использовать существующий сеанс, а если он не работает - создаст новый.
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();
}
})();