How to disable Javascript when using Selenium?


We can disable JavaScript using Selenium webdriver. We have to use the Options class to achieve this task. Firstly, we have to create an object of the Options class.

Then apply the set_preference method on that object. For disabling the JavaScript, we shall set the browser parameter javascript.enabled to False. Also this information should be passed to the driver object.

Syntax

op = Options()
op.set_preference('javascript.enabled', False)

We can get the javascript.enabled parameter of the browser by following the below steps −

  • Open a browser.

  • Type about:config in browser address.

  • Enter javascript in the search bar.

Example

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
#object of Options class
op = Options()
#disable JavaScript
op.set_preference('javascript.enabled', False)
#set geckodriver.exe path
driver = webdriver.Firefox(executable_path="C:\geckodriver.exe",
options=op)
driver.maximize_window()
#launch about:config
driver.get("about:config")

Output

Updated on: 01-Feb-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements