Connect to geckodriver with selenium js

依然范特西╮ 提交于 2019-12-13 04:25:24

问题


I'm not an expert of Selenium, so I may miss something here.

  • One of the software in the corp starts a firefox with Geckodriver.
  • I would like to connect / attach to this browser from my JavaScript code.

I know the port where the Webserver starts and the sessions identifier.

I try to connect from JS:

const webdriver = require('selenium-webdriver')

void async function() {
    let driver = await new webdriver.Builder().forBrowser('firefox').usingServer('http://localhost:55849/').build();

    await driver.get('http://www.google.com/ncr');
    await driver.findElement(By.name('q')).sendKeys('webdriver');
    await driver.findElement(By.name('btnG')).click();
    await driver.wait(until.titleIs('webdriver - Google Search'), 1000);

    driver.quit();
}();

The connection is not successful. What I can think is that this code tries to start a new instance.

There is an error message:

SessionNotCreatedError: Session is already started

Any idea how I can connect to the existing one? And control it?

I've tried everything from the docs: https://www.npmjs.com/package/selenium-webdriver

I even tried to connect http://localhost:55849/wd/hub but then I received WebDriverError: HTTP method not allowed error


回答1:


Use the selenium-webdriver/http

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

let sessionId = '9aad751d-eb9b-4c92-92f3-298c733f6ec7';
let url = 'http://localhost:57538';

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


来源:https://stackoverflow.com/questions/58266549/connect-to-geckodriver-with-selenium-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!