[英]How to read the pdf file using selenium
我正在一個有鏈接的網頁上工作,單擊該鏈接會在新窗口上打開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) ,它工作正常。 它不會讀取圖像。
第一個Downlaod pdfbox jar。
strURL是一個包含.pdf文件的網絡URl:like( https://example.com/downloads/presence/Online-Presence-CA-05-02-2017-04-13.pdf )
public boolean verifyPDFContent(String strURL, String text) {
String output ="";
boolean flag = false;
try{
URL url = new URL(strURL);
BufferedInputStream file = new BufferedInputStream(url.openStream());
PDDocument document = null;
try {
document = PDDocument.load(file);
output = new PDFTextStripper().getText(document);
System.out.println(output);
} finally {
if (document != null) {
document.close();
}
}
}catch(Exception e){
e.printStackTrace();
}
if(output.contains(text)){
flag = true;
}
return flag;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.