User Agents

Web browsers and everyday web tech, explained

Disable JavaScript in Headless Chrome with Selenium Python

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Disabling JavaScript in headless Chrome with Selenium Python lets you test how a page behaves without client-side scripting, speed up scraping, or verify that content is server-rendered. This guide shows you exactly how to configure ChromeOptions to turn off JavaScript while running in headless mode.

What You'll Need

  • Python 3.6 or newer installed on your system.
  • The Selenium package installed (pip install selenium).
  • Chrome browser and the matching ChromeDriver executable. You can download ChromeDriver from the official site or use a package manager like webdriver-manager.
  • Basic familiarity with writing and running Python scripts.

Step-by-Step

  1. Import Selenium modules. Start your script with the necessary imports: from selenium import webdriver and from selenium.webdriver.chrome.options import Options.
  2. Create a ChromeOptions instance. Instantiate an Options() object. This object lets you set various browser preferences before launching Chrome.
  3. Add the headless argument. Use options.add_argument('--headless') to run Chrome without a visible window. In newer Chrome versions, you may also use --headless=new for the new headless mode, but the classic flag still works.
  4. Disable JavaScript via Chrome preferences. The most reliable way is to set the profile.managed_default_content_settings.javascript preference to 2 (where 2 means blocked). Use options.add_experimental_option('prefs', {'profile.managed_default_content_settings.javascript': 2}).
  5. Optional: Disable images and other content. If you want to further reduce resource loading, you can also set profile.managed_default_content_settings.images to 2 in the same prefs dictionary.
  6. Create the driver with these options. Pass the options object to webdriver.Chrome(options=options). Make sure ChromeDriver is in your PATH or provide the path explicitly via executable_path (deprecated in newer Selenium versions, so prefer PATH).
  7. Test that JavaScript is off. Navigate to a page that uses JavaScript to display content, or use a simple test like driver.execute_script('return 1+1'). If JavaScript is disabled, this will throw an exception. Alternatively, check driver.execute_script('return typeof jQuery') on a page that would normally load jQuery, and it should return undefined.
  8. Run your script and handle errors. Since many modern sites rely on JavaScript, you may encounter missing elements or timeouts. Use explicit waits with WebDriverWait only if you expect some content to appear after a delay, but remember that without JavaScript, dynamic content won't load.

Common Mistakes

  • Using the wrong preference key. Some tutorials suggest javascript.enabled (that's for Firefox) or profile.content_settings.exceptions.javascript. For Chrome, the correct key is profile.managed_default_content_settings.javascript.
  • Forgetting to add the headless argument. Without it, Chrome will open a visible window, which may not be what you want for automation.
  • Setting the preference after creating the driver. Preferences must be set on the Options object before the driver is instantiated. Changing them later has no effect.
  • Assuming all JavaScript is disabled. The preference disables JavaScript for the default content settings, but some browser extensions or internal pages may still run scripts. For most practical purposes, it works, but don't rely on it for security.
  • Using an outdated ChromeDriver. If your ChromeDriver version doesn't match your Chrome browser version, the driver may fail to start or behave unexpectedly.

Tips

  • Combine with other options for faster scraping. Disable images, CSS, and other resources by setting additional preferences like profile.managed_default_content_settings.images to 2 or using --blink-settings=imagesEnabled=false.
  • Use a user-agent string. Some sites serve different content to headless browsers. Set a common user-agent via options.add_argument('--user-agent=...') to avoid being blocked.
  • Test with a simple page first. Use a basic HTML page with a <noscript> tag to confirm that JavaScript is off. For example, a page that shows "JavaScript is ON" via script and "JavaScript is OFF" via noscript.
  • Consider using a context manager. Use with webdriver.Chrome(options=options) as driver: to ensure the driver is properly closed even if an error occurs.

Troubleshooting

  • JavaScript still runs. Double-check the preference key spelling. Also, ensure you're not using an older Selenium version that might ignore experimental options. Update Selenium and ChromeDriver to the latest versions.
  • ChromeDriver fails to start. This often happens due to version mismatch. Check your Chrome version (chrome://settings/help) and download the matching ChromeDriver. Alternatively, use webdriver-manager to automatically handle versions.
  • Page content is blank. Without JavaScript, many sites show nothing because they rely on client-side rendering. This is expected. Use a site that has server-rendered content, or check the raw HTML with driver.page_source.
  • Errors about 'unknown error: cannot find Chrome binary'. This means Selenium can't locate the Chrome executable. Specify the path using options.binary_location = '/path/to/chrome'.
  • Headless mode not working. In some environments, you might need to add --no-sandbox and --disable-dev-shm-usage (especially in Docker or CI). Add these arguments to your options.

Frequently Asked Questions

Does disabling JavaScript affect all pages in the same browser session?

Yes, once set, the preference applies to all pages loaded by that driver instance. If you need to enable JavaScript later, you'll need to create a new driver with different options.

Can I disable JavaScript only for certain domains?

Yes, but it's more complex. You can use Chrome's content settings exceptions via the profile.content_settings.exceptions.javascript preference, but it requires a specific format. For most automation, it's easier to use a separate driver instance per domain.

Will disabling JavaScript speed up my scraping?

Often, yes. Many sites load heavy scripts that delay page load. Without JavaScript, pages may load faster, but you'll miss any content that is rendered dynamically.

Is there a way to verify JavaScript is disabled without executing a script?

You can check driver.capabilities for the javascriptEnabled capability. It should be False when disabled. However, this capability may not always reflect the preference, so testing with a known page is more reliable.

Does this work with Chrome for Testing or Chromium?

Yes, the same options work with any Chromium-based browser, including Chrome for Testing and Chromium. Just ensure you have the corresponding driver.

Can I use this in headless mode on a server without a display?

Absolutely. The headless mode is designed for server environments. You may need to add --no-sandbox if running as root, and --disable-dev-shm-usage to avoid shared memory issues in containers.