7

I am using Chrome headlessly.

I tried setting the --disable-javascript command line argument.

I tried using the experimental options:

        $options->setExperimentalOption('prefs', [
            'profile.managed_default_content_settings.javascript' => 2//this does not work
            //,'profile.default_content_setting_values.javascript' => 2//this does not work, too
        ]);

        $capabilities = DesiredCapabilities::chrome();
        $capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

As of this point these two do not work.

How can I disable javascript in Chrome using the Facebook PHP Webdriver ?

Here is a test to check if JavaScript is enabled:

        $this->driver->get('https://www.whatismybrowser.com/detect/is-javascript-enabled');
        return [
            $this->driver->getTitle(),
            $this->driver->findElement(WebDriverBy::cssSelector('.detected_result'))->getText()
        ];
5
  • Check this Discussion/QA -how to disable Java script in browser using java selenium automation? Commented Nov 2, 2017 at 2:52
  • 1
    I tried your example with the preferences and javascript is disabled as expected (Win10, Chromium 62, driver 2.33). Another one is 'profile.content_settings.exceptions.javascript.*.setting' => 2. Make sure that you don't have any policies (chrome://policy/) overriding your prefs. Commented Nov 24, 2017 at 13:55
  • Thanks @FlorentB. for checking it on Windows. It's good to know it works. It's been almost a month since I starded using Firefox instead. I only have a single Seelenium set up at the moment (Ubuntu 16.04 Vagrant box ). I will spin some Ubuntu 16.04 up with GUI to check the default chrome://policy/ later. Is it possible that there are some default proxy policy settings on a fresh Chrome install ? I mean ... it's a fresh non-X11 install, fresh Chrome install, running the browser headlessly .... and none of these settings work. I am left with trying the * one. Will write later ... Commented Nov 26, 2017 at 5:09
  • 1
    @Boris D. Teoharov, disabling JavaScript is currently not supported when Chrome is launched headlessly. According to this ticket it doesn't load the preferences: bugs.chromium.org/p/chromium/issues/detail?id=775911#c7 Commented Nov 27, 2017 at 19:32
  • @FlorentB. it seems this answers my question. Please, add an answer below so I can give you the bounty prize. Commented Nov 28, 2017 at 15:35

4 Answers 4

4

It is simply impossible. Read here http://yizeng.me/2014/01/08/disable-javascript-using-selenium-webdriver/

WARNING: Running without JavaScript is unsupported and will likely break a large portion of the ChromeDriver's functionality. I suspect you will be able to do little more than navigate to a page. This is NOT a supported use case, and we will not be supporting it. Closing this as WontFix - the ChromeDriver (and every other WebDriver implementation I'm aware of) require JavaScript to function.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there fresher info on this matter ?
I am afraid that information is still accurate. I searched the Chromedriver docs and there is no reference on how to disable JS. I also checked the chrome flags but there isn't an option anymore to disable JS via command line.
This information is no longer accurate. It possible to drive Chrome with Selenium with JavaScript disabled in the page. Scripts (atoms) injected by selenium still work and it's also possible to run your own script with execute_script. It is however not supported at this time by the headless mode.
3
+150

It's possible to disable the execution of Javascript by setting one of the these preferences :

"webkit.webprefs.javascript_enabled": false
"profile.content_settings.exceptions.javascript.*.setting": 2
"profile.default_content_setting_values.javascript": 2
"profile.managed_default_content_settings.javascript": 2

But it's currently not supported headlessly since this mode doesn't load the preferences and there's no command switch related to this feature.

Note that disabling JavaScript used to break Selenium since most of commands are atom scripts injected in the page. It's no longer the case. All the commands are able to run. However I noticed that the returned text doesn't include the text from a <noscript> element (text displayed only when JavaScript is disabled). One workaround is to read the innerText property with either execute_script or get_attribute.

1 Comment

Where do I set these preferences in Chrome?
1

Although regular headless mode in Chrome prevents disabling JavaScript, there's a newer headless mode without restrictions.

The Chromium developers recently added a 2nd headless mode that functions the same way as normal Chrome.

The NEW way: --headless=chrome

The OLD way: --headless

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

This means that you can now disable JavaScript when using the newer headless mode.

7 Comments

Can this be done using chrome CLI (--headless) without selenium?
Yes, Chromium args can be set when launching Chrome from the command-line.
What would be the CLI arg to run chrome headless without javascript support? I need to disable any script execution.
there's no such flag --disable-javascript
You can use this to disable JS: chrome://settings/content/javascript
|
0

As others have pointed, it is still NOT possible to disable JavaScript in Chrome when headless.

However, for future reference, this is how you do it (when you are NOT using the --headless argument):

        $options = new ChromeOptions();

        $options->setExperimentalOption('prefs', [
            'profile.managed_default_content_settings.javascript' => 2,
        ]);

        $result = DesiredCapabilities::chrome();

        $result->setCapability(
            ChromeOptions::CAPABILITY_W3C,
            // There is a bug in php-webdriver, so ->toArray() is needed!
            $options->toArray()
        );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.