問題描述
在我的功能自動化中,我需要在瀏覽器中禁用 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!