Launching Selenium with JavaScript Disabled

This article was first published on Python | datawookie , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

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")

Looks good. Success!

Note: This has been tested with:

  • selenium/standalone-chrome-debug:3.141.
To leave a comment for the author, please follow the link and comment on their blog: Python | datawookie .

Want to share your content on python-bloggers? click here.