【问题标题】:How to disable JavaScript in browser using Selenium (Java)?如何使用 Selenium (Java) 在浏览器中禁用 JavaScript?
【发布时间】:2017-10-23 06:17:26
【问题描述】:

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

尝试了 Firefox 和 Chrome 的 DesiredCapabilities。

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

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

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

2) 添加插件 - noScript.xpi

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

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

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

【问题讨论】:

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


【解决方案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')

【讨论】:

  • @Pankaj 很高兴能帮到你)))
  • 我正在使用 selenium-java 版本 3.141.0 和 options.addPreference("javascript.enabled", false); //有效
【解决方案2】:

您可以使用具有很多选项的配置文件更改偏好值:

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

@见

【讨论】:

  • 此代码现在适用于 firefox,public void testDisableJS() throws Exception { WebElement element; driver.get("about:config"); driver.findElement(By.id("warningButton")).sendKeys(Keys.ENTER); element = driver.findElement(By.id("warningButton"));动作动作 = 新动作(驱动程序); act.moveToElement(element).click().perform(); act.sendKeys(Keys.RETURN).sendKeys("javascript.enabled").perform();线程.sleep(1000); act.sendKeys(Keys.TAB).sendKeys(Keys.RETURN).perform();线程.sleep(1000); driver.get("google.com"); }
【解决方案3】:

根据 Selenium 3.6 Java Client Release,在浏览器中禁用 Javascript 的最简单方法是设置 setJavascriptEnabled 参数通过 DesiredCapabilitiesFalse 并将其与 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 设置。

【讨论】:

  • @Saravana 在 StackOverflow 我们说 Thanks by Accepting 最好的 AnswerUpvoting Answers 帮助解决了您的问题。
  • 知道了@DebanjanB,将在未来的转换中跟进
  • 我使用的 selenium 版本不支持 FirefoxOptions(),猜测应该升级。所以无法尝试这个选项。现在也不想升级。升级后肯定会尝试这个选项。
  • 是的,这就是为什么我的答案统计为Selenium 3.6 Java Client Release 但你没有提到你的Selenium 版本。
【解决方案4】:

相信我,这是随机试验,但对我来说非常有效

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

【讨论】:

  • 问题:"javascript": 2 .... 数字 2 是什么意思?禁用? 0, 1, 3 ,4,.... 是什么意思?
【解决方案5】:

这行得通:

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

【讨论】:

    【解决方案6】:

    这就是您可以在 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 2017-10-10
      • 2010-11-10
      • 1970-01-01
      • 2023-01-03
      • 2016-10-07
      • 1970-01-01
      相关资源
      最近更新 更多