保存PDF而不是在Selenium中打开

时间:2015-08-07 06:30:06

标签: google-chrome firefox selenium-webdriver webdriver

我使用的代码总是下载PDF。从最近开始,它开始在浏览器中打开PDF。 chrome和firefox也是如此。

在Chrome中我已经尝试过了:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-extensions"));
driver = new ChromeDriver(capabilities);

在firefox中我尝试过:

FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser. download. manager. useWindow",true);
firefoxProfile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf");
firefoxProfile.setPreference("browser.download.dir","C:\\Documents and Settings\\xxxx\\My Documents\\Downloads");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf;text/plain;text/csv");
firefoxProfile.setPreference("pdfjs.disabled", true);
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force",false);
firefoxProfile.setPreference("plugin.scan.plid.all",false);
firefoxProfile.setPreference("plugin.scan.Acrobat","99.0");

但是,两个浏览器都在打开PDF而不是保存它。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

我可以向您展示我们如何在Ruby中完成它,并希望您可以将其翻译为适合您的Java(?)代码。最重要的是找出要设置的偏好键。

Capybara.register_driver :selenium_chrome_downloads do |app|
  prefs = {
    plugins: {
      plugins_disabled: ['Chrome PDF Viewer']
    },
    download: {
      prompt_for_download: false,
      default_directory: 'desired/download/path'
    }
  }
  Capybara::Selenium::Driver.new(app, browser: :chrome, prefs: prefs)
end
Capybara::Session.new(:selenium_chrome_downloads)

这会映射到首选字符串,例如" plugins.plugins_disabled"," download.prompt_for_download"和" download.default_directory"

有用的文档: https://sites.google.com/a/chromium.org/chromedriver/capabilities(主要是Java) https://code.google.com/p/selenium/wiki/RubyBindings(对于Ruby)

答案 1 :(得分:1)

基本上如下更改选项:

// this will make automatically download to the default folder.
chromeOptions.AddUserProfilePreference("plugins.always_open_pdf_externally", true);

C# Selenium Saving pdf page

答案 2 :(得分:0)

较新的解决方案:

  1. 打开Chrome

  2. 转到about:plugins

  3. 点击"禁用" Chrome Pdf Viewer插件中的链接

  4. 完成您需要的所有步骤。

  5. 您可以创建网络请求并保存对文件的响应

    c#示例从url获取图片:

    string webPath = "http://www.someasress.com/asdf.png";
    
                    if (webPath != string.Empty)
                    {
                        try
                        {
                            System.Net.WebRequest request =
                                System.Net.WebRequest.Create(webPath);
    
                            System.Net.WebResponse response = request.GetResponse();
    
                            System.IO.Stream responseStream = response.GetResponseStream();
    
                            Bitmap bitmapImg = new Bitmap(responseStream);
    
                            return bitmapImg;
                        }
                        catch (System.Net.WebException)
                        {
                        }
                    }