Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Disable Javascript in Chrome (-headless) using PHP Webdriver

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()
        ];
like image 432
Boris D. Teoharov Avatar asked Nov 02 '17 02:11

Boris D. Teoharov


People also ask

How do I disable JavaScript on a website?

Press Ctrl + Shift + P (Windows, Linux) or Command + Shift + P (macOS) to open the Command Menu. Start typing javascript , select Disable JavaScript, and then press Enter to run the command. JavaScript is now disabled.

How do I disable script debugging in Chrome?

Here are the steps. Go down to the Debugging > General option on the left side of the menu. Find and uncheck the Enable Javascript debugging for ASP.NET(Chrome and IE). Now, no new Chrome instance will be launched every time you run or debug your web project.


2 Answers

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.

like image 60
Homewrecker Avatar answered Sep 27 '22 20:09

Homewrecker


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.

like image 37
Florent B. Avatar answered Sep 27 '22 20:09

Florent B.