Selenium Webdriver语言 - 帮助PDF到文本转换



下面的代码是完美下载PDF。现在我想把这个PDF内容转换成文本文件。请帮助。我用眼睛看了很多代码,但没有一个成功。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
 @Test
 public class PDF_Download_without_popup {
 WebDriver driver;
 @BeforeTest
 public void StartBrowser() {
  //Create object of FirefoxProfile in built class to access Its properties.
  FirefoxProfile fprofile = new FirefoxProfile();
   //Set Location to store files after downloading.
  fprofile.setPreference("browser.download.dir", "c:\WebDriverdownloads");
  fprofile.setPreference("browser.download.folderList", 2);
//Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
  fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"//MIME types Of MS Excel File.
    + "application/pdf;" //MIME types Of PDF File.
    + "application/vnd.openxmlformats-officedocument.wordprocessingml.document;" //MIME types Of MS doc File.
    + "text/plain;" //MIME types Of text File.
    + "text/csv"); //MIME types Of CSV File.
  fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
  fprofile.setPreference( "pdfjs.disabled", true );
  //Pass fprofile parameter In webdriver to use preferences to download file.
  driver = new FirefoxDriver(fprofile);  
 }  
  public void OpenURL() throws InterruptedException{
     driver.get("http://www.bell.ca/");
     driver.manage().window().maximize();
     Thread.sleep(30000);
     driver.findElement(By.xpath(".//*[@id='demoLoginLinkJs']/span[1]")).click();
     driver.findElement(By.xpath(".//*[@id='USER']")).sendKeys("bell_56789");
     driver.findElement(By.xpath(".//*[@id='PASSWORD']")).sendKeys("sunday21");
     driver.findElement(By.xpath(".//*[@id='demoLoginJs']")).click();
     driver.findElement(By.xpath("//span[contains(text(),'View current bill')]")).click();
     Thread.sleep(5000);

     driver.findElement(By.xpath(".//*[@id='btnDownloadBill']")).click();
     String tmp= driver.getCurrentUrl().toString();
     System.out.println(tmp);
     Thread.sleep(50000);

 }
 @AfterTest
 public void CloseBrowser() {  
  driver.quit();   
 }
}

尝试使用Apache PDFBox API。

然后将其添加到您的项目中。

在您的情况下,您正在下载PDF,但不下载它,在navigate.to()中给出在浏览器中打开PDF的URL,例如:http://www.bell.ca/xyz.pdf。所以,你的代码应该是这样的:

URL xyzUrl = new URL("http://www.bell.ca/xyz.pdf");
BufferedInputStream TestFile = new BufferedInputStream(xyzUrl.openStream());
PDDocument xyzPDF = PDDocument.loadNonSeq(TestFile, null);
String testText = new PDFTextStripper().getText(xyzPDF);
xyzPDF.close();

现在您已经从PDF文件中获取了所有文本,并且可以使用第三方API(如Apache POI或任何其他可用的API)将这些文本写入外部XLS或任何相关类型文件。

@Geetanjali,我可以建议另一种方法。有几个在线网站谁提供PDF到文本转换服务。在那里你只需要上传你的文件,然后点击"转换",然后你的PDF将转换为文本

所以,我的观点是你也可以在每次下载a时自动执行它pdf。下载pdf后,打开其中一个网站。上传使用第三方工具,如AutoIT API(添加在您的构建路径)。并可下载转换后的文本文件。