如何通过JAVA使用Selenium时禁用Javascript? [英] How to disable Javascript when using Selenium by JAVA?

查看:83
本文介绍了如何通过JAVA使用Selenium时禁用Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒进行网络测试的JAVA。
我想在Firefox浏览器,Google Chrome浏览器,IE浏览器中停止JavaScript。



我在Firefox浏览器上试过这段代码。 b

  FirefoxProfile profile = new FirefoxProfile(); 
profile.setPreference(javascript.enabled,false);
WebDriver driver = new FirefoxDriver(profile);

但是它在第二行返回这个错误。

 线程main中的异常java.lang.IllegalArgumentException:可能不会覆盖javascript.enabled:freeze value = true,请求的值= false 

如何在每个浏览器中使用Selenium时禁用Javascript?
如果您知道这个问题,请帮助我!

解决方案

使用 Firefox的noscript插件。在添加到Firefox 按钮和保存链接上右击,它将提供保存 .xpi的选项。然后,如下配置 Firefox 探查器。

  FirefoxProfile配置文件=新的FirefoxProfile(); 
profile.AddExtension(@PATH\TO\\\
oScript.xpi);
IWebDriver driver = new FirefoxDriver(profile);

driver.Navigate()。GoToUrl(http:// localhost:8080 /);


I'm using selenium for web test by JAVA. I want to stop JavaScript on Firefox Browser,Google Chrome Browser,IE Browser.

I tried this code on Firefox Browser.

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
WebDriver driver = new FirefoxDriver(profile);

But it's return this error on second line.

Exception in thread "main" java.lang.IllegalArgumentException: Preference javascript.enabled may not be overridden: frozen value=true, requested value=false

How to disable Javascript when using Selenium each Browser? If you know about this problem,Please help me!

解决方案

Use noscript addon for firefox. Right click on the Add to Firefox button and Save link as that will give you the option to save the .xpi. Then, configure the the Firefox profiler as follows.

FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension(@"PATH\TO\noScript.xpi");
IWebDriver driver = new FirefoxDriver(profile);

driver.Navigate().GoToUrl("http://localhost:8080/");

这篇关于如何通过JAVA使用Selenium时禁用Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆