如何在Selenium中禁用Chrome PDF查看器,当出现任何PDF时,它应该在默认下载中自动下载



我正在浏览一个网站,当我点击一个按钮时,它应该下载pdf....

我正在使用最新版本的chrome 60,硒3.4,chromedriver。

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("plugins.plugins_disabled", new String[] {"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);
WebDriver driver = new ChromeDriver(cap);

我也使用了上面的代码,但它不起作用。

对我有用的是添加:

chromePrefs.put("plugins.always_open_pdf_externally", true);

我希望这有帮助

/*ChromeOptions options = new ChromeOptions();
/*use to disable the "Chrome is handled by automation script" pop up.*/
options.addArguments("disable-infobars");  
/* use to start the Chrome browser as maximized.*/
options.addArguments("--start-maximized"); 
/* HashMap is use to disable the password saving pop up in Chrome browser.*/
Map<String, Object> prefs = new HashMap<String, Object>(); 
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
/*Download File store in Download folder*/
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", downloadFilesPath);
prefs.put("plugins.always_open_pdf_externally", true);
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.setExperimentalOption("prefs", prefs);
options.addArguments("--test-type");
options.addArguments("--disable-extensions"); //to disable browser extension 
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(cap);*

最新更新