DEV Community

Andrew B. Collier
Andrew B. Collier

Posted on • Originally published at datawookie.dev on

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
Enter fullscreen mode Exit fullscreen mode

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
  }
)
Enter fullscreen mode Exit fullscreen mode

Now create a browser instance.

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

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

browser.get("https://www.whatismybrowser.com/detect/is-javascript-enabled")
Enter fullscreen mode Exit fullscreen mode

Looks good. Success!

Note: This has been tested with:

  • selenium/standalone-chrome-debug:3.141.

Top comments (0)