【问题标题】:how to disable chrome pdf viewer in selenium and it should auto download in the default downloads when any pdf occurs如何在 selenium 中禁用 chrome pdf 查看器,当出现任何 pdf 时,它应该在默认下载中自动下载
【发布时间】:2017-08-16 15:20:59
【问题描述】:

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

我正在使用最新版本的 chrome 60、selenium 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);

上面的代码我也用过,但是不行。

【问题讨论】:

标签: google-chrome selenium webdriver selenium-chromedriver chrome-options


【解决方案1】:

对我有用的是添加:

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

希望对你有帮助

【讨论】:

  • 这只是为我打开 Acrobad Reader。虽然它不会在 Chrome 中打开预览,但它根本不会跳过打开 PDF
【解决方案2】:
/*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);*

【讨论】:

  • 请考虑格式化您的帖子。这很难理解。
  • 仅代码的答案不如讨论代码为何能解决问题的答案有用。请编辑您的答案以添加更多详细信息。
猜你喜欢
  • 2022-09-27
  • 2017-05-25
  • 2018-07-05
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 2022-10-08
  • 1970-01-01
  • 2018-06-08
相关资源
最近更新 更多