在浏览器中禁用JavaScript

在浏览器中禁用JavaScript

本文介绍了如何使用Selenium(Java)在浏览器中禁用JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的功能自动化中,我需要在浏览器中禁用JavaScript并运行流程。如何禁用JavaScript?

为Firefox和Chrome尝试了DesiredCapabilities。

  DesiredCapabilities dc = new DesiredCapabilities(); 
dc.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT,false)

  DesiredCapabilities dc = new DesiredCapabilities(); 
dc.setJavascriptEnabled(false);

对于firefox,尝试
1)为firefox设置配置文件

2)添加插件 - noScript.xpi


$ b profile.setPreference(javascript.enabled,false);



4)通过UI,尝试将about:config中的标志 - javascript.enabled更改为false。在这里,打开Firefox并给about:config发出警告 - 这可能会使您的保修无效。有一个按钮 - 我会小心的,我保证!用id - warningButton。这个按钮应该点击进一步进行。点击这个按钮,使用driver.findElement(By.id(warningButton))。click();但它不起作用。



以上所有选项都不起作用。任何建议都会有帮助。

解决方案

使用包含许多选项的配置文件来更改首选项值:

DesiredCapabilities capabilities = new DesiredCapabilities();
// setCapability(SUPPORTS_JAVASCRIPT,javascriptEnabled);
capabilities.setJavascriptEnabled(false);

FirefoxBinary binary = new FirefoxBinary(new File(binaryPath));
FirefoxProfile配置文件=新的FirefoxProfile();

//profile.setPreference(\"preferenceName,Value);
profile.setPreference(javascript.enabled,false);

RemoteWebDriver驱动=新的FirefoxDriver(binary,profile,capabilities);

要查看偏好设置,可以访问 URL about:config





@See






In my feature automation, I need to disable JavaScript in browser and run the flow. How to disable JavaScript?

Tried DesiredCapabilities for firefox and Chrome.

DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, false)

And

DesiredCapabilities dc = new DesiredCapabilities();
dc.setJavascriptEnabled(false);

For firefox, tried1) Setting up profile for firefox

2) Adding add-on - noScript.xpi

3) profile.setPreference("javascript.enabled", false);

4) Through UI, tried changing the flag - "javascript.enabled" in "about:config" to false. Here, opened firefox and gave "about:config" getting a warning - "This might void your warranty!". There is a button - "I'll be careful, I promise!" with id - warningButton. This button should be clicked to proceed further. To click this button, used driver.findElement(By.id("warningButton")).click(); but it not work.

All the above options are not working. Any advice will be helpful.

解决方案

You change the preference value using profile with lots of options:

DesiredCapabilities capabilities = new DesiredCapabilities();
// setCapability(SUPPORTS_JAVASCRIPT, javascriptEnabled);
capabilities.setJavascriptEnabled(false);

FirefoxBinary binary = new FirefoxBinary( new File( binaryPath ) );
FirefoxProfile profile = new FirefoxProfile();

//profile.setPreference("preferenceName", "Value");
profile.setPreference("javascript.enabled", false);

RemoteWebDriver driver = new FirefoxDriver(binary, profile, capabilities);

To view the preferences, you can visit the URL about:config

@See

这篇关于如何使用Selenium(Java)在浏览器中禁用JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 16:41