How to disable Javascript when using Selenium by JAVA? - javascript

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/");

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

Accessibility disabled pop up window impact Selenium Firefox WebDriver test. How to solve it?

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();

How to access chrome extensions using selenium

I am new to selenium, I have a selenium script that works fine for chrome, I could add the the extension to the chrome using the .crx file but how can I use the extension while testing with selenium. For Instance I have 'Resource Override' extension which replaces one of my javascript file with the custom file. I cannot open inspect element and trigger the extension as chrome driver does not allow us to do so.
1.I am not sure how can I get the extension to use my javascript file using selenium. The extension has a text field to provide a path to the file that needs to be replaced and and a text box to add custom javascript code.
2.Is it possible to dynamically have the application use my custom javascript file using selenium with out using mentioned extension. I want to find alternatives with out checking in my code.
System.setProperty("webdriver.chrome.driver","/Path/chromedriver");
System.setProperty("webdriver.chrome.logfile", "/Path/sel_logs.log");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
LoggingPreferences loggingprefs = new LoggingPreferences();
loggingprefs.enable(LogType.BROWSER, Level.ALL);
capabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingprefs);
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/Path/0.6.2_0.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://google.com");

JS off with "profile.set_preference("javascript.enabled", False)" for firefox(v35) doesn't work for selenium 47 and above. Is there any other way? [duplicate]

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

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.

Categories

Resources