How To Disable JavaScript Using Custom Profile For selenium WebDriver Test

Many times you need to disable JavaScript In your browser to verify that function or validation Is working fine even If JavaScript Is disabled. Simple example Is you need to disable JavaScript of Firefox browser to check server side validation of Input text field. Enabling or disabling JavaScript Is
simple task If you will do It manually but how to do same thing In selenium webdriver test?

As you know, Selenium webdriver launch fresh browser every time so your current browser's settings will not work In webdriver launched browser. So we need to write some special code In test case to disable JavaScript run time. Earlier I have provided example of how to enable or disable JavaScript In selenium IDE on THIS POST. Now let us learn how to do It In selenium WebDriver.

WebDriver has very good feature which allows us to create custom profile for firefox browser and then we can set/update browser's settings run time. Earlier we have set and used browser's custom profile run time to download different files as described In THIS POST. We will use same thing here to disable javascript.

Bellow given syntax will create new firefox profile and set JavaScript disabled.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);

and then we can use that custom profile In webdriver launched browser using bellow given syntax.
WebDriver driver = new FirefoxDriver(profile);

So now your webdriver browser's JavaScript Is disabled. To verify It practically, Run bellow given example In eclipse. It will click on Show Me Alert button but due to the disabled Javascript, Alert will not not prompted. Used checkAlertPresent() function to verify If alert Is present or not on page.

package Testing_Pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class jscript {
 WebDriver driver;
 public void setup() throws Exception {
  //Create new firefox custom profile.
  FirefoxProfile profile = new FirefoxProfile();
  //Disable javascript for newly created profile.
     profile.setPreference("javascript.enabled", false);
     //Use custom profile which has javascript disabled In webdriver launched browser.
     driver = new FirefoxDriver(profile);     
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 public void getCoordinates(){
  //Click on button to get Javascript alert.
  driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
  //Check If alert Is present or not.
   //If alert present then bellow given code will be executed.
   Alert alert = driver.switchTo().alert();
   System.out.println("Alert present");
 public boolean checkAlertPresent(){
    //It will return true If alert present.
    return true;
   }catch(NoAlertPresentException ex){
    //It will return false If alert not present and print bellow given message In console.
    System.out.println("No Alert present. Verify If javascript Is disabled.");
    return false;

This way you can disable javascript In your selenium webdriver test using custom profile.


  1. Firstly thanks a lot for sharing your knowledge and really well explained in a simple view.

  2. Hi Sir,Can you please provide the to disable JavaScript in Internet Explorer.

  3. Very nice !!!!!!!!!!!!!!!!

  4. I found this error while setting preference:

    Exception in thread "main" java.lang.IllegalArgumentException: Preference javascript.enabled may not be overridden: frozen value=true, requested value=false

  5. Even I am also getting the same Error Preference javascript.enabled may not be overridden: frozen value=true, requested value=false

  6. Preference javascript.enabled may not be overridden: frozen value=true, requested to solve this issue

  7. profile.setPreference("javascript.enabled", true);

  8. Or else u can make it as profile.setPreference("javascript.disabled",true);

  9. or else make it as profile.setPreference("javascript.disabled", true)

  10. I am getting the error as

    java.lang.IllegalArgumentException: Preference javascript.enabled may not be overridden: frozen value=true, requested value=false

  11. Looks like the command is no longer working pro.setPreference("javascript.enabled", false);
    EVen tried pro.setPreference("javascript.disabled", true); none of them works with latest version of selenium webdriver 3