【问题标题】:How to read PDF contents in selenium如何在 selenium 中阅读 PDF 内容
【发布时间】:2020-06-23 02:38:57
【问题描述】:

我正在尝试验证 PDF 中的内容,我正在使用 href 获取 URL 并将其传递到下面的代码中。 URL 使用 HTTPS,所以我面临以下问题。任何人都可以帮助我如何继续并帮助我阅读 pdf 数据。提前致谢

重试网址是https://XXXXXXXXXXXXXXXXX/apex/DA_ViewArchive?docType=pdf&docid=2229123

        URL PDFUrl = new URL(url);
        BufferedInputStream TestFile = new BufferedInputStream(PDFUrl.openStream());
        PDFParser TestPDF = new PDFParser((RandomAccessRead) TestFile);
        TestPDF.parse();
        String TestText = new PDFTextStripper().getText(TestPDF.getPDDocument());
        System.out.println("Document Text is   "+   TestText);

错误是

java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)

【问题讨论】:

标签: java html selenium selenium-webdriver pdf-parsing


【解决方案1】:

您是否在所需的驱动程序功能中设置了接受 SSL 证书?

DesiredCapabilities dc = DesiredCapabilities.chrome ()       
dc.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true)
WebDriver driver = new ChromeDriver (dc);

【讨论】:

  • 嗨@rrgirish,我没有使用它。
  • 您的堆栈跟踪显示 SSL 异常,您需要在 webdriver 中设置 SSL 证书,以上代码适用于 chrome。这篇文章似乎有其他浏览器的所有代码guru99.com/ssl-certificate-error-handling-selenium.html
  • 不知道有没有用,但我首先想到的是是否需要代理设置(企业环境?)
【解决方案2】:

首先下载包含所有依赖项的 pdfbox JAR 2.0.13 并将其导入。 现在从 URL 读取 PDF 文件。

public String readPDFInURL(String text) throws EmptyFileException, IOException {
        System.out.println("Enters into READ PDF");
        String output = "";
        URL url = new URL(driver.getCurrentUrl());
        System.out.println("url :  " + url);
        InputStream is = url.openStream();
        BufferedInputStream fileToParse = new BufferedInputStream(is);
        PDDocument document = null;
        try {
            document = PDDocument.load(fileToParse);
            output = new PDFTextStripper().getText(document);
            if (output.contains(text)) {
                System.out.println("Element is matched in PDF is : " + text);
                test.log(LogStatus.INFO, "Element is displayed in PDF " + text);
            } else {
                System.out.println("Element is not  matched in PDF");
                test.log(LogStatus.ERROR, "Element is not displayed in PDF :: " + text);
                throw new AssertionError("Element is not displayed" + text);
            }
        } finally {
            if (document != null) {
                document.close();
            }
            fileToParse.close();
            is.close();
        }
        return output;
    }

【讨论】:

  • 此答案没有帮助,OP 存在您未尝试解决的通信问题(超时)。并且不要使用 2.0.13。当前版本是 2.0.19。
【解决方案3】:

您可以使用 Maven 添加 pdfbox jar 依赖项,并开始阅读使用 Selenium 下载的 pdf 或现有的 pdf 文档。

例如:

  File file = new File("C:/PdfBox_Examples/new.pdf");
  PDDocument document = PDDocument.load(file);

  //Instantiate PDFTextStripper class
  PDFTextStripper pdfStripper = new PDFTextStripper();

  //Retrieving text from PDF document
  String text = pdfStripper.getText(document);
  System.out.println(text);

  //Closing the document
  document.close();

【讨论】:

  • 这个答案没有帮助,OP 有一个你没有尝试解决的通信问题(超时)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
相关资源
最近更新 更多