Connect to already started web driver with selenium js - javascript

I'm not an expert of Selenium, so I may miss something here.
One of the software in the corp starts a google chrome with ChromeDriver.
I would like to attach to this browser from my JavaScript code.
I know the port where the ChromeDriver starts:
Starting ChromeDriver 77.0.3865.10 (bc3579f611bbc73331171afe020ec7a45e6ccc55-refs/branch-heads/3865#{#93}) on port 55848
I try to connect from JS:
const webdriver = require('selenium-webdriver')
void async function() {
let driver = await new webdriver.Builder().forBrowser('chrome').usingServer('http://localhost:55848/').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 not created: This version of ChromeDriver only supports Chrome version 77
I checked that the running chrome version is 77 and the ChromeDriver is also 77. This Chrome that is started by the corp software is actually a portable version of Chrome. I have a Chrome 76 installed on my computer. What I can think is that the code I wrote tries to start a new instance of Chrome. And there the version does not match.
Any idea how I can connect to the existing one? And control it?
UPDATE:
I managed to do the same with Firefox. The Firefox is started with geckodriver. I still cannot connect it. The error message is:
SessionNotCreatedError: Session is already started
So I'm pretty sure that is not in connection with the chrome versions but it tries to create a new session instead of connecting the existing one.

Yes, the problem is with the version of chrome driver. Get chromedriver.exe version 77... or else you will keep getting this error as latest version of selenium 3.141.59 doesnt support other version of chrome. Go to this link (https://www.seleniumhq.org/download/) and get the chromedriver.exe latest version and I think it will solve your problem.

Error - SessionNotCreatedError: session not created: This version of ChromeDriver only supports Chrome version 77
The error is related to mismatch between chromedriver.exe and chromebrowser version.
So based on browser version installed in operating system we have, we can download chromedriver.exe from below link https://chromedriver.chromium.org/downloads
Actual question answer -
Yes, we can connect to an existing running selenium server by setting desired capabilities provided by chrome. Then passing this capability while creating selenium session
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "77.0.3865.10:55848");
WebDriver driver = new ChromeDriver(options);
The only thing to be searched is equivalent javascript code for doing same as above code is using java binding with selenium. I will surely update this answer with js binding.
#Edit=>
This is something I was able to find the way to pass chrome options in javascript. But sadly I didn't found setExperimentalOptions method here inside ChromeOptions. So I have used addArguments method as shown below.
const { Options } = require('selenium-webdriver/chrome');
const options = new Options();
options.addArguments('debuggerAddress=77.0.3865.10:55848');
builder.setChromeOptions(options);
const driver = builder.build();
driver.get('url');

Related

Using JavaScript's Selenium Webdriver, how to launch Chrome with a specific path to chromedriver.exe?

I have the following Javascript code that launches Chrome with the path to chromedriver.exe specified by the PATH environment variable. This question is different than how to launch Chrome with a specific chrome.exe because chromedriver.exe is a standalone server that takes in commands from Selenium Server and uses JSON commands to talk to the Chrome browser's API. You need both chrome.exe and chromedriver.exe to launch a Chrome browser with Selenium.
let driver = await new Builder()
.forBrowser('chrome')
.build();
However, I would like to clear my PATH environment variable and instead set a path from within the code that launches Chrome, something like the below. How can I do that please?
let driver = await new Builder()
.forBrowser('chrome')
.withDriverPath('C:\\temp\\chromedriver.exe')
.build();
In reading https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/chrome.js
At line 212
It seem posible to set in your environ variables a value for CHROMEDRIVER_EXE

How to disable Javascript when using Selenium by JAVA?

I'm using selenium for web test by JAVA.
I want to stop JavaScript on Firefox Browser,Google Chrome Browser,IE Browser.
I tried this code on Firefox Browser.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
WebDriver driver = new FirefoxDriver(profile);
But it's return this error on second line.
Exception in thread "main" java.lang.IllegalArgumentException: Preference javascript.enabled may not be overridden: frozen value=true, requested value=false
How to disable Javascript when using Selenium each Browser?
If you know about this problem,Please help me!
Use noscript addon for firefox. Right click on the Add to Firefox button and Save link as that will give you the option to save the .xpi. Then, configure the the Firefox profiler as follows.
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension(#"PATH\TO\noScript.xpi");
IWebDriver driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl("http://localhost:8080/");

WebSocket to localhost not working on Microsoft Edge

I've created a simple WebSocket server and am trying to connect to it with the following code:
function test(name) {
var ws = new WebSocket('ws://localhost:1234');
ws.onopen = function () {
ws.send('Hello from ' + name);
}
}
test('Edge');
This works fine from Chrome and IE11 on Windows10 but when I try from Edge, the onopen function isn't called, instead I eventually get the following error:
SCRIPT12029: WebSocket Error: Network Error 12029, A connection with the server could not be established
This is happening for version 12.10240 of Edge.
A similar problem was asked about here but the error message is different.
Things I tried:
IP - localhost, 127.0.0.1, the actual IP
Allow localhost loopback flag - both on and off
When trying to communicate with a different machine the problem does not occur.
Is this a defect in Edge or am I doing something wrong?
I had a similar problem, but it was when actually navigating to pages in the browser. I could use the phrase localhost and it worked fine, but I didn't work when I used my machine's name. I found this forum where they suggested that you run the following command in a administrator prompt:
CheckNetIsolation LoopbackExempt -a -n=Microsoft.MicrosoftEdge_8wekyb3d8bbwe
Worked for me. Thought you might be seeing some form of the same issue.
As the forum suggests, you can now also do this by opening Edge and navigating to "about:flags", then tick "Allow localhost loopback (this might put your device at risk)" under "Developer Settings". Should feel a little safer than pasting random stuff into your command prompt.
Edit: As tresf pointed out below, the loopback checkbox in the about:flags appears to have stopped working (as a fix), so you'll have use the CheckNetIsolation command, to make Edge exempt.

unable to open chrome using webdriver in javascript

I got a very weird problem when trying out webdriverjs on my windows machine and would like your help or suggestion on this one. I follow the instruction online, first npm install selenium-webdriver, then download chromedriver and configure its path. Before proceed to testing I double check the installation, chrome and firefox are working properly and when running "chromedriver" on cmd it also works correctly "Starting ChromeDriver 2.14.313457 on port 9515 Only local connections are allowed." So i assume the system setup is correct. Then I tried the first simple example using js. Below is my code:
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.firefox()).
build();
driver.get('http://www.google.com/ncr');
driver.sleep(10000);
driver.quit();
This works perfectly fine with firefox,and firefox is opened and directed to the google page. However, when i switch to the second example by using chrome, the chrome never opened and no error messages showed, it just stuck there. Here is the second example I used, the only difference from the first one is changing firefox to chrome
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('http://www.google.com/ncr');
driver.sleep(10000);
driver.quit();
I dont know why chrome is not opened by webdriver, i searched the internet for some answers but didn't find any.
Here comes the more weird part. I changed my code to build a firefox-driver first, and then build the chrome-driver, code shown below
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.firefox()).
build();
var driver_2 = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver_2.sleep(10000);
driver_2.quit();
In this way, both firefox and chrome is opened. So my question is "why chrome is not opened unless i build a firefox before it"???
Please give me some suggestions on this, or is that some setup in my computer is wrong? Thanks for all your help!!!
The setup is correct, but the way you using chrome-driver is incorrect. After running chrome-driver, it will show you the port it runs on, by default it is port 9515. Then in your code you should use "usingServer",
var driver = new webdriver.Builder().
usingServer('http://localhost:9515').
withCapabilities(webdriver.Capabilities.chrome()).
build();
to access chromedriver. That way chrome-driver can work correctly.

Can I/how can I translate a Selenium webdriver test script from node.js over to phantomjs - ghostdriver?

I recently began working with Selenium and to make life easier to start I was using node to run my scripts so that I could visually monitor the tests. My challenge now is to convert it so that it can be run as a headless test. Unfortunately, most of the resources that I have come across only address using phantomjs and ghostdriver with Java or Python. My boss wants me to run the test through phantomjs without Java or Python. Eventually these tests will be run remotely through a Linux VM on a server without a GUI. Currently I am testing using Mac OS X 10.8 and still have many bridges to cross in order to get to my goal.
My most important question firstly, is it possible to run a script from phantomjs through a port without the use of Java or Python? I have spent hours poring through as many resources as I could come across and I've come up with no solution.
If so, how can I properly initialize the test to run headless? Here is how I scripted the start of my functioning test. I want to properly switch the capabilities from firefox to phantomjs and be able to run it headless using the appropriate port. The rest of the test navigates to a specific site, logs in through a widget, then does further navigation to the area which I will build further tests on which to manipulate after I get this working.
var webdriver = require('selenium-webdriver'),
SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
var server = new SeleniumServer("Path/selenium-server-standalone-2.39.0.jar", {
port: 8910
});
server.start();
var driver = new webdriver.Builder().
usingServer(server.address()).
withCapabilities(webdriver.Capabilities.firefox()).
build();
The test works perfectly, but I am new to this so there might be something foolish that I am overlooking. Please let me know what adjustments to make so that it will run headless through phantom. When I attempt to use node to run the script after switching capabilities to phantomjs it produces
"/Selenium/node_modules/selenium-webdriver/phantomjs.js:22
LogLevel = webdriver.logging.LevelName,
^
TypeError: Cannot read property 'LevelName' of undefined
at Object.<anonymous> (/Selenium/node_modules/selenium-webdriver/phantomjs.js:22:33)
That's a read only file that I can't adjust, any attempts that I made to define "LogLevel" or "LevelName" to the appropriate corresponding value (DEBUG, etc.) were fruitless.
And if I run it through phantomjs itself I get -
"Error: Cannot find module 'path'
phantomjs://bootstrap.js:289
phantomjs://bootstrap.js:254 in require"
(It also lists module 'http') -- (and various undefined function errors)
I feel that with that instance I didn't properly organize where the files for Selenium, phantomjs, and ghostdriver should go in order to play nice. I also removed the server setup portion and instead ran this first, then the script separately.
phantomjs --webdriver=8910
But it yielded the same result. All of my research to fix these issues turned up instructions for Java and Python but not Javascript by itself. Rather than chase through many rabbit holes I figured it wise to consult better minds.
If you know better than I do and that it is fruitless to attempt this without Java or Python, please let me know. If you know where the issue lies within my script and could propose a fix please let me know. I hope that I have properly described the nature of my issue and if you need more information I will do my best to provide it to you.
This is my second week working with Javascript so if you believe I am making a noob error you very well may be correct. Please, keep in mind that the script works through node with selenium webdriver.
Many thanks for your time!!!
~Isaac
This was a bit tricky but here is the solution I've pieced together:
var webdriver = require('selenium-webdriver'),
SeleniumServer = require('selenium-webdriver/remote').SeleniumServer,
server = new SeleniumServer('/path/to/selenium/selenium-server-standalone-2.41.0.jar', {
port: 4444
}),
capabilities = webdriver.Capabilities.phantomjs();
capabilities.set('phantomjs.binary.path', 'path/to/phantom/bin/phantomjs');
var promise = server.start().then(function() {
var client = new webdriver.Builder().
usingServer(server.address()).withCapabilities(
capabilities
).build();
return {
'client': client,
'server': server
};
}, function(err) {
console.log('error starting server', err);
});
You can then use the promise with selenium's mocha-compatible test framework to hold the test till the server has started.
I found the documentation really helpful once i figured out the navigation is on the far right of the page. Here's the URL: http://selenium.googlecode.com/git/docs/api/javascript/module_selenium-webdriver.html
Then you'll be stuck where I am. Getting selenium-webdriver to quiet down.

Categories

Resources