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.
Related
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');
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
I am using Selenium 3.0.1 for automation test on Firefox Driver (51 version). Every time firefox pops up this window, it impacts the testing process.
I searched online for a solution. Most people suggest to do the following steps:
1. Visit about: config
2. Create a new boolean preference named browser.tabs.remote.force-enable and set its value as true, and change accessibility.force_disabled preference value to 1, from 0.
I tired, the preference value can be successfully changed/created when I directly launched firefox. But when running the selenium java code, the accessibility window still comes out. Check the preference value, they did not get changed. I could not change it neither in this selenium driven webpage.
Could anyone help solve this problem? Thanks very much.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.tabs.remote.force-enable", true);
profile.setPreference("accessibility.force_disabled",1);
System.setProperty("webdriver.gecko.driver", "D:/D-Download/geckodriver/geckodriver.exe");
WebDriver driver = new FirefoxDriver(profile);
// WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://jqueryui.com/datepicker/");
driver.switchTo().frame(0);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
// Click on textbox so that datepicker will come
driver.findElement(By.id("datepicker")).click();
I am using below code to disable java script in C# Selenium webdriver with specflow.
FirefoxProfile p = new FirefoxProfile();
p.SetPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);
I am not able disable javascript for firefox browser.
Can you please help me where I am wrong.
Thanks,
Saurabh
There were multiple suggestions to use FirefoxProfile or DesireCapabilities to accomplish that. Those also never work for me. What did is the answer from a brilliant SO user #alecxe. See this
Luckily, there are people who do some great work for us so we can take the benefit. Use this add-on and that will solve your problem. And configuring that is real simple. Just 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(#"D:\Users\Saifur\Desktop\noScript.xpi");
IWebDriver driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl("https://stackoverflow.com/");
And, just to show you it's working I did a Scrrencast
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/");