Launch Chrome with JS disabled for Selenium (Python)

You may be creating automated tests on a project which needs to function with JavaScript disabled, this is quite uncommon these days but its still worth planning for.

Here’s how its done:

from selenium import webdriver
import platform, os

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}
CHROME_VERSION = os.getenv('CHROME_VERSION')

# Pass in the options we just set
driver = webdriver.Chrome("..\webdrivers\chromedriver.exe", options=options)

driver.get("https://www.msn.com")
print('> Chrome launched with JS disabled')


Posted

in

,

by

Tags: