6

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()
        ];
Boris D. Teoharov
  • 2,319
  • 4
  • 30
  • 49
  • Check this [**Discussion/QA -how to disable Java script in browser using java selenium automation?**](https://stackoverflow.com/questions/46883024/how-to-disable-java-script-in-browser-using-java-selenium-automation/46886271#46886271) – undetected Selenium Nov 02 '17 at 02: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. – Florent B. Nov 24 '17 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 ... – Boris D. Teoharov Nov 26 '17 at 05: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: https://bugs.chromium.org/p/chromium/issues/detail?id=775911#c7 – Florent B. Nov 27 '17 at 19:32
  • @FlorentB. it seems this answers my question. Please, add an answer below so I can give you the bounty prize. – Boris D. Teoharov Nov 28 '17 at 15:35

4 Answers4

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.

Homewrecker
  • 1,076
  • 1
  • 15
  • 38
  • Is there fresher info on this matter ? – Boris D. Teoharov Nov 24 '17 at 21:25
  • 1
    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. – Homewrecker Nov 26 '17 at 14:31
  • 2
    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. – Florent B. Nov 27 '17 at 19:22
3

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.

Florent B.
  • 41,537
  • 7
  • 86
  • 101
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()
        );
Boris D. Teoharov
  • 2,319
  • 4
  • 30
  • 49
0

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.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48