【问题标题】:Save PDF instead of opening in Selenium保存 PDF 而不是在 Selenium 中打开
【发布时间】:2015-10-30 12:09:12
【问题描述】:

我使用的代码总是可以下载 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 而不是保存它。

有什么想法吗?

【问题讨论】:

标签: google-chrome firefox selenium-webdriver webdriver


【解决方案1】:

基本上改变如下选项:

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

C# Selenium Saving pdf page

【讨论】:

  • 不客气,如果有帮助,请投票。谢谢。
【解决方案2】:

我可以向您展示我们是如何在 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)

【讨论】:

    【解决方案3】:

    较新的解决方案:

    1. 打开铬

    2. 转到 about:plugins

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

    4. 根据需要执行所有步骤。


    您可以创建网络请求并将响应保存到文件

    从url获取图片的c#示例:

    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)
                        {
                        }
                    }
    

    【讨论】:

    • 其实没有文件的url,提交后才收到。它也是动态的。
    • @user3905599,添加了另一个解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多