2

Using these options together doesn't work as expected:

options.add_argument('--disable-javascript')
options.add_argument('--headless')

When I disable JavaScript without using headless mode, it looks like this.

But when I try to disable JavaScript with headless mode, it looks like this.

1
  • it is still NOT possible to disable JavaScript in Chrome when headless. Commented Oct 5, 2022 at 11:54

1 Answer 1

7

It's now possible to disable JavaScript in headless Chrome if you use the new headless mode:

The new way (2023): --headless=new (Chrome 109 and above)

The previous way: --headless=chrome (Chrome 96 - Chrome 108)

(The old way: --headless)

There's more info on that here: https://bugs.chromium.org/p/chromium/issues/detail?id=706008#c36

For the updated Chrome 109 info, see: https://github.com/chromium/chromium/commit/e9c516118e2e1923757ecb13e6d9fff36775d1f4

You can use the code below as options for completely disabling JavaScript while running headlessly:

prefs = {}
prefs["webkit.webprefs.javascript_enabled"] = False
prefs["profile.content_settings.exceptions.javascript.*.setting"] = 2
prefs["profile.default_content_setting_values.javascript"] = 2
prefs["profile.managed_default_content_settings.javascript"] = 2

options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", prefs)
options.add_argument('--disable-javascript')
options.add_argument('--headless=new')
Sign up to request clarification or add additional context in comments.

3 Comments

I'm so sorry , it reminds me that "Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded." I'm a noob , one day i have enough reputation , I will go back to vote for you. Really really thanks!
This options.add_argument('--headless=chrome') was key for me to work. I had options.headless = True
Starting with Chrome 109, use --headless=new. (They just changed it)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.