如何使用Java在Selenium WebDriver中禁用Chrome插件

时间:2016-06-03 14:30:51

标签: java google-chrome selenium plugins

Chrome Plugin pop up

当我为此应用程序执行自动化代码时,会显示上面的弹出窗口。现在我需要知道如何使用Java在Selenium WebDriver中禁用PDF Viewer插件。

这就是我现在正在使用的无效工作。

 DesiredCapabilities capabilities = DesiredCapabilities
                                .chrome();
                        ChromeOptions options = new ChromeOptions();
                        options.addArguments(new String[] { "test-type" });
                        options.addArguments(new String[] { "disable-extensions" });


String pluginToDisable = "Chrome PDF Viewer";
                        options.addArguments("plugins.plugins_disabled", pluginToDisable);


                        capabilities.setCapability("chrome.binary",
                                chromeDriver.getAbsolutePath());
                        capabilities.setCapability(ChromeOptions.CAPABILITY,
                                options);
                        options.addArguments("--lang=en-gb");
                        GlobalVars.driver = new ChromeDriver(capabilities);

5 个答案:

答案 0 :(得分:5)

针对Chrome V更新了:57

此解决方案不再有效。

这是C#中的有效实现:

function isVowel(x) {
  return (['a', 'e', 'i', 'o', 'u', 'y'].indexOf(x.toLowerCase()) > -1);
};

console.log(isVowel("c"));
console.log(isVowel("a"));
console.log(isVowel("A"));

答案 1 :(得分:3)

以下是使用Selenium / Chrome禁用Flash和PDF查看器的示例:

ChromeOptions options = new ChromeOptions();
Map<String, Object> preferences = new Hashtable<String, Object>();
options.setExperimentalOption("prefs", preferences);

// disable flash and the PDF viewer
preferences.put("plugins.plugins_disabled", new String[] {
    "Adobe Flash Player",
    "Chrome PDF Viewer"
});

// launch the browser and navigate to the page
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.google.co.uk");

答案 2 :(得分:1)

似乎&#34; plugins.plugins_disabled&#34;或者&#34; plugins.plugins_list&#34;不再工作了。选项&#34; plugins.always_open_pdf_externally&#34;为我工作(mheirendt's解决方案)。 Java中的示例:

ChromeOptions co = new ChromeOptions();
Map<String,Object> prefs = new HashMap<>();
prefs.put("plugins.always_open_pdf_externally", true);
co.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(co);

答案 3 :(得分:0)

---这对我有用 -

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
                    chromePrefs.put("plugins.plugins_disabled", new String[] {"Adobe Flash Player", "Chrome PDF Viewer"});
                    chromePrefs.put("profile.default_content_settings.popups", 0);
                    ChromeOptions options = new ChromeOptions();
                    options.setExperimentalOption("prefs", chromePrefs);
                    DesiredCapabilities cap = DesiredCapabilities.chrome();
                    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
                    cap.setCapability(ChromeOptions.CAPABILITY, options);
                    GlobalVars.driver = new ChromeDriver(cap);

答案 4 :(得分:0)

由于Chrome的设置页面也是一个网络应用程序,您可以通过编程方式抓取设置页面并禁用PDF查看器。这是一个示例python代码:

browser = webdriver.Chrome("./chromedriver")    
browser.get("chrome://settings-frame/content")
pdf_section = browser.find_element_by_id("pdf-section")
pdf_disable_checkbox = pdf_section.find_element_by_tag_name("input")
if not pdf_section.is_selected():
    pdf_disable_checkbox.click()