如何使用 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')
相关文章