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.

2
  • this answer works for Firefox 61.0.1 Commented Aug 4, 2018 at 9:47
  • option simpler than the one above. work for modern versions of firefox Commented Mar 27, 2019 at 23:44

6 Answers 6

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')
Sign up to request clarification or add additional context in comments.

1 Comment

I am using selenium-java version 3.141.0 and options.addPreference("javascript.enabled", false); //works
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

1 Comment

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("google.com"); }
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.sstatic.net/DdKZQ.png

1 Comment

Question: "javascript": 2 .... what does the number 2 means? Disable? And what would 0, 1, 3 ,4,.... mean?
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.

4 Comments

@Saravana On StackOverflow we say Thanks by Accepting the best Answer and by Upvoting the Answers which helped in solving your question.
Got it @DebanjanB, will follow in the future conversions
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.
Yeap, that's why my Answer stats with Selenium 3.6 Java Client Release but you haven't mentioned your Selenium version.
0

this works:

FirefoxOptions options = new FirefoxOptions();      
options.addPreference("javascript.enabled", false);

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.