How to disable JavaScript in browser using Selenium (Java)?
Solution 1
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')
Solution 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
@See
Solution 3
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 DesiredCapabilities
to False
and merge it with 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.
Saravana
Updated on June 04, 2022Comments
-
Saravana almost 2 years
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.
-
Levon over 5 yearsthis answer works for
Firefox 61.0.1
-
AtachiShadow about 5 yearsoption simpler than the one above. work for modern versions of firefox
-
-
Saravana over 6 yearsThis 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"); }
-
undetected Selenium over 6 years@Saravana On StackOverflow we say
Thanks
byAccepting
the bestAnswer
and byUpvoting
theAnswers
which helped in solving your question. -
Saravana over 6 yearsGot it @DebanjanB, will follow in the future conversions
-
Saravana over 6 yearsthe 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.
-
undetected Selenium over 6 yearsYeap, that's why my Answer stats with
Selenium 3.6 Java Client Release
but you haven't mentioned yourSelenium
version. -
AtachiShadow about 5 years@Pankaj I'm glad I could help you)))
-
Dan Sin almost 5 yearsI am using selenium-java version 3.141.0 and options.addPreference("javascript.enabled", false); //works
-
Kyon Perez about 2 yearsQuestion: "javascript": 2 .... what does the number 2 means? Disable? And what would 0, 1, 3 ,4,.... mean?