问题
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