2

In my feature automation, I need to disable JavaScript in browser and run the flow. How to disable JavaScript?

Tried DesiredCapabilities for firefox and Chrome.

DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, false)

And

DesiredCapabilities dc = new DesiredCapabilities();
dc.setJavascriptEnabled(false);

For firefox, tried 1) Setting up profile for firefox

2) Adding add-on - noScript.xpi

3) profile.setPreference("javascript.enabled", false);

4) Through UI, tried changing the flag - "javascript.enabled" in "about:config" to false. Here, opened firefox and gave "about:config" getting a warning - "This might void your warranty!". There is a button - "I'll be careful, I promise!" with id - warningButton. This button should be clicked to proceed further. To click this button, used driver.findElement(By.id("warningButton")).click(); but it not work.

All the above options are not working. Any advice will be helpful.

Boris D. Teoharov
  • 2,319
  • 4
  • 30
  • 49
Saravana
  • 21
  • 1
  • 1
  • 4
  • [this answer](https://stackoverflow.com/a/51681608/2248627) works for `Firefox 61.0.1` – Levon Aug 04 '18 at 09:47
  • [option simpler](https://stackoverflow.com/a/53054654/7685008) than the one above. work for modern versions of firefox – AtachiShadow Mar 27 '19 at 23:44

6 Answers6

5

I don't know Java, but maybe a solution for Python 3 will help you.

in Python, you can use Options() instead of FirefoxProfile() to deactivate JavaScript:

from selenium.webdriver.firefox.options import Options
options = Options()
options.preferences.update({"javascript.enabled": False})
driver = webdriver.Firefox(options=options)
driver.get('about:config')

Maybe Java this:

FirefoxOptions options = new FirefoxOptions();
options.preferences.update({"javascript.enabled": False});
WebDriver driver = new FirefoxDriver(options);
driver.get('about:config')
AtachiShadow
  • 381
  • 4
  • 13
  • I am using selenium-java version 3.141.0 and options.addPreference("javascript.enabled", false); //works – Dan Sin Jun 29 '19 at 01:25
2

You change the preference value using profile with lots of options:

DesiredCapabilities capabilities = new DesiredCapabilities();
// setCapability(SUPPORTS_JAVASCRIPT, javascriptEnabled);
capabilities.setJavascriptEnabled(false);

FirefoxBinary binary = new FirefoxBinary( new File( binaryPath ) );
FirefoxProfile profile = new FirefoxProfile();

//profile.setPreference("preferenceName", "Value");
profile.setPreference("javascript.enabled", false);

RemoteWebDriver driver = new FirefoxDriver(binary, profile, capabilities);

To view the preferences, you can visit the URL about:config

enter image description here

@See

Yash
  • 9,250
  • 2
  • 69
  • 74
  • This code works now for firefox,public void testDisableJS() throws Exception { WebElement element; driver.get("about:config"); driver.findElement(By.id("warningButton")).sendKeys(Keys.ENTER); element = driver.findElement(By.id("warningButton")); Actions act = new Actions(driver); act.moveToElement(element).click().perform(); act.sendKeys(Keys.RETURN).sendKeys("javascript.enabled").perform(); Thread.sleep(1000); act.sendKeys(Keys.TAB).sendKeys(Keys.RETURN).perform(); Thread.sleep(1000); driver.get("https://www.google.com"); } – Saravana Oct 23 '17 at 10:15
1

As per Selenium 3.6 Java Client Release, the easiest way to disable Javascript in the browser would be to set the setJavascriptEnabled argument through an instance of DesiredCapabilities to False and merge it through an instance of FirefoxOptions as follows:

package demo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Q46883024_setJavascriptEnabled 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setJavascriptEnabled(false);
        FirefoxOptions op = new FirefoxOptions();
        op.merge(dc);
        WebDriver driver = new FirefoxDriver(op);
        driver.get("https://google.com");
        driver.quit();
    }
}

While execution, the browser you are using may override the setJavascriptEnabled settings.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @Saravana On StackOverflow we say **`Thanks`** by **`Accepting`** the best **`Answer`** and by **`Upvoting`** the **`Answers`** which helped in solving your question. – undetected Selenium Oct 23 '17 at 11:05
  • Got it @DebanjanB, will follow in the future conversions – Saravana Oct 24 '17 at 04:29
  • the selenium version which i use is not supporting FirefoxOptions(), guessing it should be upgraded. So unable to try this option. Also not want to upgrade now. Will definitely try this option after upgrade. – Saravana Oct 24 '17 at 07:07
  • Yeap, that's why my Answer stats with `Selenium 3.6 Java Client Release` but you haven't mentioned your `Selenium` version. – undetected Selenium Oct 24 '17 at 08:28
1

Truse me this was random trial but works perfectly for me

from selenium import webdriver

options= webdriver.ChromeOptions()

chrome_prefs = {}
options.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"javascript": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"javascript": 2}
driver = webdriver.Chrome("your chromedriver path here",options=options)

driver.get('https://google.com/search?q=welcome to python world')

Example image here:-https://i.stack.imgur.com/DdKZQ.png

iampritamraj
  • 196
  • 2
  • 6
0

this works:

FirefoxOptions options = new FirefoxOptions();      
options.addPreference("javascript.enabled", false);
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
Pankaj Kumar Katiyar
  • 1,474
  • 1
  • 20
  • 36
0

This is how you can do it for Chrome in Java.

// import org.openqa.selenium.chrome.ChromeOptions;

ChromeOptions options = new ChromeOptions();
options.addArguments("user-agent=\"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)\"");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_setting_values.javascript", 2);
options.setExperimentalOption("prefs", chromePrefs);
new ChromeDriver(options);

And it worked for me with ChromeDriver 2.41.578706. As a bonus I am also setting Googlebot as user-agent.

In case you need to do something with DesiredCapabilities you can also convert the options above to capabilities:

// import static org.openqa.selenium.chrome.ChromeOptions.CAPABILITY;

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CAPABILITY, options);
new ChromeDriver(capabilities);
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
Mahdi
  • 1,778
  • 1
  • 21
  • 35