sodcalculator

10 Tech How-To

Run Selenium Tests with JavaScript Disabled

Guide 10-44573 · Updated · Step by step

This guide shows you how to configure Selenium WebDriver to run your tests with JavaScript disabled in the browser. This is useful for checking that your web application’s core functionality works without client-side scripting, which helps catch accessibility issues, SEO problems, and progressive enhancement failures. You’ll learn the exact code and browser options needed for Chrome, Firefox, and Edge.

What You’ll Need

  • A working Selenium WebDriver setup (Python, Java, C#, or JavaScript) with the appropriate browser driver installed (e.g., ChromeDriver, GeckoDriver, EdgeDriver).
  • Access to a test project where you can modify or add test scripts.
  • Basic familiarity with your chosen programming language and Selenium’s API.

Step-by-Step

  1. Choose your browser and driver. The approach differs slightly by browser. Chrome and Edge use the --disable-javascript command-line argument. Firefox uses a preference setting. Ensure you have the correct driver version matching your browser version.
  2. For Chrome (and Edge): Create a new ChromeOptions (or EdgeOptions) object and add the argument --disable-javascript. In Python:
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument("--disable-javascript")
    driver = webdriver.Chrome(options=options)

    In Java:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-javascript");
    WebDriver driver = new ChromeDriver(options);
  3. For Firefox: Use a FirefoxOptions object and set the preference javascript.enabled to false. In Python:
    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.set_preference("javascript.enabled", False)
    driver = webdriver.Firefox(options=options)

    In Java:

    FirefoxOptions options = new FirefoxOptions();
    options.addPreference("javascript.enabled", false);
    WebDriver driver = new FirefoxDriver(options);
  4. Create a test that verifies JavaScript is disabled. A simple check is to execute a script that returns window.navigator or attempts to set a variable. For example, you can use driver.execute_script("return window.navigator.userAgent;") – this will still work because Selenium’s own injection is not blocked, but you can verify that page-specific scripts don’t run by checking for a DOM element that JavaScript would normally create. Better: navigate to a page that has a <noscript> tag and assert it is visible.
  5. Write your test logic. Once the driver is initialized with JavaScript disabled, proceed with your normal Selenium commands (find elements, click, type, etc.). Remember that any action relying on JavaScript (e.g., clicking a button that only has a JS event listener) will not work. You may need to use Actions or direct URL navigation to simulate user behavior.
  6. Run your test suite. Execute your tests as usual. The browser will open with JavaScript disabled for the entire session. After the test, always close the driver with driver.quit() to free resources.

Common Mistakes

  • Using the wrong option for Firefox. Firefox does not support --disable-javascript as a command-line argument; you must use the preference. Mixing them up will not disable JavaScript.
  • Forgetting to close the driver. Leaving the browser open can cause resource leaks and interfere with subsequent tests.
  • Assuming all JavaScript is disabled. Some browsers still run inline event handlers (like onclick attributes) even with JavaScript disabled. The --disable-javascript flag disables most script execution, but not all. For a stricter test, consider using a browser profile with JavaScript fully disabled.
  • Not checking for the <noscript> content. If your page has a <noscript> fallback, your test should verify that it is displayed, not just that the page loads.

Tips

  • Use a separate test class or test suite for JavaScript-disabled tests so you can run them independently from your main suite.
  • If you need to test both enabled and disabled states, parameterize your tests to run with different browser options.
  • When checking for JavaScript-disabled behavior, prefer waiting for elements with explicit waits (e.g., WebDriverWait) rather than implicit waits, because pages may load slower without JS.
  • For debugging, take a screenshot when a test fails to see exactly what the user would see without JavaScript.

Troubleshooting

  • JavaScript is still running. Double-check that you applied the option to the correct driver instance. In Chrome, if you have multiple profiles, the argument may be overridden. Try using a fresh user-data-dir: options.add_argument("--user-data-dir=/tmp/test-profile").
  • Browser won’t start with the option. Some versions of Chrome have removed the --disable-javascript flag. If that happens, use a browser profile with JavaScript disabled via the profile’s preferences. For Chrome, you can set the preference profile.content_settings.exceptions.javascript to block all sites, but this is more complex. Alternatively, use Firefox with the preference method, which is more reliable.
  • Tests fail because elements are not found. Without JavaScript, many dynamic elements won’t exist. Check the page’s static HTML and adjust your selectors to target <noscript> content or server-rendered elements.
  • Page loads but nothing works. Verify that the browser actually has JavaScript disabled by navigating to a test page that shows window.navigator or by running driver.execute_script("return 1;") – if that returns an error, JavaScript is disabled. If it returns 1, the flag didn’t take effect.

Frequently Asked Questions

Does this work for all browsers?

Chrome, Edge, and Firefox support disabling JavaScript via the methods above. Safari does not have a simple command-line flag; you would need to disable JavaScript in the browser’s preferences manually, which is not recommended for automated tests.

Will Selenium’s own JavaScript commands still work?

Yes. Selenium’s execute_script and execute_async_script commands are injected via the driver and are not blocked by disabling page JavaScript. You can still use them for assertions, but they won’t reflect the user’s ability to run scripts.

Can I run a single test with JavaScript disabled without affecting others?

Yes, by creating a new driver instance with the disabled options only for that test. Each test that needs JavaScript disabled should create its own driver with the appropriate options.

Why would I want to test with JavaScript disabled?

It helps ensure your website is accessible to users who have JavaScript turned off (e.g., for security or data savings), and it validates that your core content and navigation are not dependent on client-side scripting. It’s also a good practice for SEO because search engines may not execute JavaScript.

Is there a performance impact?

Running tests without JavaScript can actually be faster because the browser doesn’t load or execute scripts. However, you may need to add explicit waits for static content to load, which can offset the speed gain.

What if my application uses a JavaScript framework like React or Angular?

Most modern frameworks render the entire UI via JavaScript, so disabling JavaScript will likely cause the page to appear blank. In that case, you should test the server-rendered fallback (if any) or consider that your application may not support no-JS users.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →