如何使用Webdriver在Chrome中禁用Javascript(-headless)

时间:2022-01-22 23:40:47

I am using Chrome headlessly.

我无头地使用Chrome。

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

我尝试设置--disable-javascript命令行参数。

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 ?

如何使用Facebook PHP Webdriver在Chrome中禁用javascript?

Here is a test to check if JavaScript is enabled:

以下是检查JavaScript是否已启用的测试:

        $this->driver->get('https://www.whatismybrowser.com/detect/is-javascript-enabled');
        return [
            $this->driver->getTitle(),
            $this->driver->findElement(WebDriverBy::cssSelector('.detected_result'))->getText()
        ];

2 个解决方案

#1


1  

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

可以通过设置以下偏好之一来禁用Javascript的执行:

"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.

请注意,禁用用于破坏Selenium的JavaScript,因为大多数命令都是在页面中注入的原子脚本。现在不再是这样了。所有命令都能够运行。但是我注意到返回的文本不包含

#2


3  

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

这根本不可能。在这里阅读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.

警告:不支持在没有JavaScript的情况下运行,这可能会破坏ChromeDriver的大部分功能。我怀疑你能够做的只是导航到一个页面。这不是受支持的用例,我们不会支持它。将其关闭为WontFix - ChromeDriver(以及我所知道的所有其他WebDriver实现)都需要JavaScript才能运行。

#1


1  

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

可以通过设置以下偏好之一来禁用Javascript的执行:

"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.

请注意,禁用用于破坏Selenium的JavaScript,因为大多数命令都是在页面中注入的原子脚本。现在不再是这样了。所有命令都能够运行。但是我注意到返回的文本不包含

#2


3  

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

这根本不可能。在这里阅读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.

警告:不支持在没有JavaScript的情况下运行,这可能会破坏ChromeDriver的大部分功能。我怀疑你能够做的只是导航到一个页面。这不是受支持的用例,我们不会支持它。将其关闭为WontFix - ChromeDriver(以及我所知道的所有其他WebDriver实现)都需要JavaScript才能运行。