如何保存在没有网址的浏览器窗口中弹出的.pdf文件?

问题描述:

我正在使用我公司网站上提供的 .pdf 文件。我不知道有任何方法可以下载它们并存储在一个文件夹中。

I am working with .pdf files that are available on my companies' website only. I am not aware of any way to download them and store in one folder.

我点击获取 .pdf $的链接c $ c> file包含以下源代码:

The link that I click to get the .pdf file has the following source code:

  <a href="javascript:propertiesView('documentName')">

当我点击链接时, .pdf 文件弹出一个没有url且没有源代码的新浏览器窗口。我认为没有办法直接操作 .pdf ,然后如何保存它然后才能操作 .pdfs 来自文件夹

As I click on the link, a .pdf file pops up in a new browser window with no url and no source code. I presume that there is no way to manipulate that .pdf directly, then how can I save it then in order to manipulate the .pdfs from a folder?

谢谢

您可以通过简单地告诉浏览器始终将PDF文件保存到磁盘来获得好运(相信 Dirk ):

You may have luck by simply telling your browser to always save PDF files to disk (credits to Dirk):

firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

如果这不起作用,您可能可以使用所有打开的窗口/标签迭代 switchTo()方法。尝试这样的方法来获得一些关于你打开的窗户的信息(归功于 Prashant Shukla ):

If that doesn't work, you are probably able to iterate through all open windows/tabs by using the switchTo() methods. Try something like this to get some insight about your opened windows (credits to Prashant Shukla):

    public void getWindows() {
        Set<String> windows = driver.getWindowHandles();

        for (String window : windows) {
            driver.switchTo().window(window);
            System.out.println(driver.getTitle());
        }
    }

下载文件的非硒解决方案将是使用apache-commons库(creadits来盛源路):

A non-selenium solution to download the file would be to use the apache-commons library (creadits to Shengyuan Lu):

org.apache.commons.io.FileUtils.copyURLToFile(URL, File)

但是这需要你知道窗口的URL,你可能用我提到的第二种方法来获取它( driver.switchTo())和 driver.getCurrentUrl()

But this would require that you know the URL of the window, which you probably are able to fetch with the second approach i mentioned (driver.switchTo()) and driver.getCurrentUrl().