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

如何使用Selenium(Java)在浏览器中禁用JavaScript?,java,selenium,firefox,selenium-webdriver,selenium-chromedriver,Java,Selenium,Firefox,Selenium Webdriver,Selenium Chromedriver,在我的FeatureAutomation中,我需要在浏览器中禁用JavaScript并运行流。如何禁用JavaScript 已尝试firefox和Chrome所需的功能 DesiredCapabilities dc = new DesiredCapabilities(); dc.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, false) 及 对于firefox,已尝试 1) 为firefox设置配置文件 2) 添加加载项-noScript.

在我的FeatureAutomation中,我需要在浏览器中禁用JavaScript并运行流。如何禁用JavaScript

已尝试firefox和Chrome所需的功能

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

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

2) 添加加载项-noScript.xpi

3) setPreference(“javascript.enabled”,false)

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


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

您可以使用具有许多选项的配置文件来更改首选项值:

DesiredCapabilities=新的DesiredCapabilities();
//setCapability(支持Java脚本,javascriptEnabled);
setJavascriptEnabled(false);
FirefoxBinary=新的FirefoxBinary(新文件(binaryPath));
FirefoxProfile profile=新的FirefoxProfile();
//profile.setPreference(“preferenceName”、“Value”);
setPreference(“javascript.enabled”,false);
RemoteWebDriver=新的FirefoxDriver(二进制、配置文件、功能);
要查看首选项,您可以访问
URL about:config

@看


根据Selenium 3.6 Java客户端版本,在浏览器中禁用
的最简单方法是通过
setJavascriptEnabled参数设置为
False
并将其与
FirefoxOptions
合并,如下所示:

package demo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Q46883024_setJavascriptEnabled 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        DesiredCapabilities dc = new DesiredCapabilities();
        dc.setJavascriptEnabled(false);
        FirefoxOptions op = new FirefoxOptions();
        op.merge(dc);
        WebDriver driver = new FirefoxDriver(op);
        driver.get("https://google.com");
        driver.quit();
    }
}
执行时,您使用的浏览器可能会覆盖
setJavascriptEnabled
设置


我不懂Java,但也许Python3的解决方案会对您有所帮助

在Python中,可以使用Options()而不是FirefoxProfile()来停用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')
也许是这样的:

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

FirefoxOptions options = new FirefoxOptions();      
options.addPreference("javascript.enabled", false);

这就是如何在Java中为Chrome做到这一点

// import org.openqa.selenium.chrome.ChromeOptions;

ChromeOptions options = new ChromeOptions();
options.addArguments("user-agent=\"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)\"");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_setting_values.javascript", 2);
options.setExperimentalOption("prefs", chromePrefs);
new ChromeDriver(options);

这是一次随机试验,但对我来说效果很好

从selenium导入webdriver
options=webdriver.ChromeOptions()
chrome_prefs={}
选项。实验选项[“prefs”]=chrome\u prefs
chrome_prefs[“profile.default_content_settings”]={“javascript”:2}
chrome_prefs[“profile.managed_default_content_settings”]={“javascript”:2}
driver=webdriver.Chrome(“此处的chromedriver路径”,options=options)
司机,上车https://google.com/search?q=welcome 到python世界')

此处的示例图像:-https://i.stack.imgur.com/DdKZQ.png

这段代码现在适用于firefox,public void testDisableJS()抛出异常{WebElement元素;driver.get(“about:config”);driver.findElement(By.id(“warningButton”)).sendKeys(key.ENTER);element=driver.findElement(By.id(“warningButton”);Actions act=new Actions(驱动程序);act.moveToElement(元素)。单击().perform();act.sendKeys(key.RETURN)。sendKeys(“javascript.enabled”).perform();Thread.sleep(1000);act.sendKeys(key.TAB)。sendKeys(key.RETURN)。perform();Thread.sleep(1000);driver.get(“);}@Saravana在StackOverflow上我们通过接受最好的答案和向上投票来表达感谢。我使用的selenium版本不支持FirefoxOptions(),猜测它应该升级。因此无法尝试此选项。现在也不想升级。升级后肯定会尝试此选项。是的,这就是为什么我的回答统计了
Selenium 3.6 Java客户端版本
,但你没有提到你的
Selenium
版本。比上面的版本更适用于
Firefox 61.0.1
。工作吧对于现代版本的firefox@Pankaj我很高兴能帮助您)我正在使用SeleniumJava版本3.141.0和options.addPreference(“javascript.enabled”,false);//工作正常
// import org.openqa.selenium.chrome.ChromeOptions;

ChromeOptions options = new ChromeOptions();
options.addArguments("user-agent=\"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)\"");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_setting_values.javascript", 2);
options.setExperimentalOption("prefs", chromePrefs);
new ChromeDriver(options);
// import static org.openqa.selenium.chrome.ChromeOptions.CAPABILITY;

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CAPABILITY, options);
new ChromeDriver(capabilities);