如何使用 Selenium (Java) 在浏览器中禁用 JavaScript?

2023-06-27Java开发问题
96

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

问题描述

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

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

尝试了 Firefox 和 Chrome 的 DesiredCapabilities.

Tried DesiredCapabilities for firefox and Chrome.

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

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

对于火狐,试过1) 为 Firefox 设置配置文件

For firefox, tried 1) Setting up profile for firefox

2) 添加插件 - noScript.xpi

2) Adding add-on - noScript.xpi

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

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

4) 通过 UI,尝试将about:config"中的javascript.enabled"标志更改为 false.在这里,打开 Firefox 并给about:config"一个警告 - 这可能会使您的保修失效!".有一个按钮——我会小心的,我保证!"带有 id - 警告按钮.应单击此按钮以继续进行.要单击此按钮,请使用 driver.findElement(By.id("warningButton")).click();但它不起作用.

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.

推荐答案

我不懂 Java,但也许 Python 3 的解决方案会对你有所帮助.

I don't know Java, but maybe a solution for Python 3 will help you.

在 Python 中,您可以使用 Options() 而不是 FirefoxProfile() 来停用 JavaScript:

in Python, you can use Options() instead of FirefoxProfile() to deactivate JavaScript:

from selenium.webdriver.firefox.options import Options
options = Options()
options.preferences.update({"javascript.enabled": False})
driver = webdriver.Firefox(options=options)
driver.get('about:config')

也许是 Java:

FirefoxOptions options = new FirefoxOptions();
options.preferences.update({"javascript.enabled": False});
WebDriver driver = new FirefoxDriver(options);
driver.get('about:config')

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

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13