How to disable Javascript when using Selenium?

SeleniumAutomation TestingTesting Tools

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

raja
Published on 01-Feb-2021 17:04:27
Advertisements