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

时间:2017-10-23 06:17:26

标签: java selenium firefox selenium-webdriver selenium-chromedriver

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

尝试了Firefox和Chrome的DesiredCapabilities。

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

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

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

2)添加附加组件 - noScript.xpi

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

4)通过用户界面,尝试更改标志 - " javascript.enabled" in" about:config"为假。在这里,打开firefox并给出了关于:config"收到警告 - "这可能会使您的保修失效!"。有一个按钮 - "我要小心,我保证!"带有id - warningButton。应单击此按钮以继续。要单击此按钮,请使用driver.findElement(By.id(" warningButton"))。click();但它不起作用。

以上所有选项均无效。任何建议都会有所帮助。

6 个答案:

答案 0 :(得分:1)

您可以使用包含许多选项的配置文件更改首选项值:

DesiredCapabilities capabilities = new DesiredCapabilities();
// setCapability(SUPPORTS_JAVASCRIPT, javascriptEnabled);
capabilities.setJavascriptEnabled(false);

FirefoxBinary binary = new FirefoxBinary( new File( binaryPath ) );
FirefoxProfile profile = new FirefoxProfile();

//profile.setPreference("preferenceName", "Value");
profile.setPreference("javascript.enabled", false);

RemoteWebDriver driver = new FirefoxDriver(binary, profile, capabilities);

要查看偏好设置,您可以访问URL about:config

enter image description here

@see

答案 1 :(得分:1)

我不懂Java,但是也许Python 3的解决方案会为您提供帮助。

在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')

也许Java这样:

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

答案 2 :(得分:0)

根据 Selenium 3.6 Java Client Release ,在浏览器中停用 Javascript 的最简单方法是设置 {{1} } 通过 setJavascriptEnabled DesiredCapabilities 进行对话,并将其与 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(); } } 设置。

答案 3 :(得分:0)

这有效:

FirefoxOptions选项=新的FirefoxOptions();
options.addPreference(“ javascript.enabled”,false);

答案 4 :(得分:0)

这是在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);

它对ChromeDriver 2.41.578706来说对我有用。另外,我还将Googlebot设置为用户代理。

如果您需要对DesiredCapabilities做一些事情,还可以将上述选项转换为功能:

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

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

答案 5 :(得分:0)

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

from selenium import webdriver

options= webdriver.ChromeOptions()

chrome_prefs = {}
options.experimental_options["prefs"] = chrome_prefs
chrome_prefs["profile.default_content_settings"] = {"javascript": 2}
chrome_prefs["profile.managed_default_content_settings"] = {"javascript": 2}
driver = webdriver.Chrome("your chromedriver path here",options=options)

driver.get('https://google.com/search?q=welcome to python world')

示例图片在这里:-https://i.stack.imgur.com/DdKZQ.png