Launching Selenium with JavaScript Disabled

I have a rather obscure situation where I want to launch Selenium… but with JavaScript disabled.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

After a bit of Googling it turns out that this is eminently possible by just setting some options prior to launching the browser.

chrome_options = Options()
chrome_options.add_experimental_option(
  "prefs",
  {
    'profile.managed_default_content_settings.javascript':2
  }
)

Now create a browser instance.

browser = webdriver.Remote(
    "http://127.0.0.1:4444/wd/hub",
    DesiredCapabilities.CHROME,
    options=chrome_options
)

Head over to https://www.whatismybrowser.com/detect/is-javascript-enabled to check.

browser.get("https://www.whatismybrowser.com/detect/is-javascript-enabled")
WhatIsMyBrowser.com showing that JavaScript is disabled.

Looks good. Success!

Note: This has been tested with:

  • selenium/standalone-chrome-debug:3.141.