java - 如何使用 Selenium 读取pdf文件

标签 java pdf selenium-webdriver download pdf-reader

我正在处理有链接的网页,单击它会在新窗口中打开一个 pdf 文件。 我必须阅读该 pdf 文件以根据已完成的交易验证一些数据。一种方法是下载该文件然后使用它。 谁能帮我解决这个问题。我必须在 IE 11 上工作

提前致谢。

最佳答案

使用 PDFBox 和 FontBox。

    public String readPDFInURL() throws EmptyFileException, IOException {
        WebDriver driver = new FirefoxDriver();
        // page with example pdf document
        driver.get("file:///C:/Users/admin/Downloads/dotnet_TheRaceforEmpires.pdf");
        URL url = new URL(driver.getCurrentUrl());
        InputStream is = url.openStream();
        BufferedInputStream fileToParse = new BufferedInputStream(is);
        PDDocument document = null;
        try {
            document = PDDocument.load(fileToParse);
            String output = new PDFTextStripper().getText(document);
        } finally {
            if (document != null) {
                document.close();
            }
            fileToParse.close();
            is.close();
        }
        return output;
    }

由于旧版本 PDFBox 的一些功能已被弃用,我们需要使用另一个 FontBox 和 PDFBox。我用过PDFBox (2.0.3)FontBox (2.0.3)它工作正常。但它不会读取图像。

关于java - 如何使用 Selenium 读取pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40738373/

相关文章:

javascript - Protractor 返回一个对象但预期 - element.getText() 的值

java - Apache Spark - foreach Vs foreachPartition 什么时候使用?

android - 如何从 intent 过滤器接收 pdf 文件 - Android

android - Google 驱动器 pdf 查看器在 android 上不再工作

javascript - 我想在上传 pdf 文件时在缩略图上显示 pdf 图标

java - 如何点击页面刷新时 id 不断变化的日期选择器

java - 如何检查 List<BasicNameValuePair> 是否包含键?

java - 在 Docker 上运行 UPNP 时遇到问题

java - 从字符串获取类类型

javascript - Selenium Webdriver-python,无法找到动态元素,所有可能的方法都试过了