我在网页上工作,点击一个链接在同一个窗口打开一个pdf文件。我必须阅读pdf文件的内容来验证一些数据。我不想下载pdf来阅读内容。我需要从浏览器本身读取内容。从url如何验证打开的文件是pdf文件。使用最新的chrome浏览器版本。有人能帮我吗?
这里的url不是以.pdf扩展名结尾。pdf url示例:blob:httpshttp://x.com/15946946-ca67-40eb-abd5-3fd8d477519d
示例代码:
String currentRawURL = driver.getCurrentUrl();
String[] currentURL = currentRawURL.split("blob:");
String PDFURL = currentURL[1];
URL url = new URL(PDFURL);
InputStream is = url.openStream();
BufferedInputStream fileParse = new BufferedInputStream(is);
PDDocument document = PDDocument.load(fileParse);
PDFTextStripper pdf = new PDFTextStripper();
String pdfContent = pdf.getText(document);
document.close();
System.out.println(" PDF Content : " +pdfContent);
// TestNG assertion for checking if text is present in content
Assert.assertTrue(pdfContent.contains("test"));
控制台输出:
java.io.IOException: Error: End-of-File, expected line
at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1119)
at org.apache.pdfbox.pdfparser.COSParser.parseHeader(COSParser.java:2497)
at org.apache.pdfbox.pdfparser.COSParser.parsePDFHeader(COSParser.java:2468)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:233)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1144)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1041)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
暂无答案!
目前还没有任何答案,快来回答吧!