2015-10-28 97 views
0

目前即時使用Selenium Webdriver for c#和PDFBox自動化報表的PDF文本。現在,使用PDFBox,我們可以通過URL = new URL(「pdf of the link」)解析PDF鏈接。但是,這不需要任何先決條件即可直接使用URL鏈接。PDF自動化使用URL從已打開的內容繼續

如果我使用直接URL地址訪問pdf,則會出現「資源未找到」錯誤。我基本上需要登錄到應用程序,並選擇我需要的報告,以加載實際的PDF。它具有相同的地址,除了它是在應用程序中加載的,而不是直接加載的。我如何將Selenium與PDFbox關聯起來,以便代替打開直接轉到拒絕權限URL的新URL,它將轉到已在屏幕上預裝的PDF報告,並使用相同的URL地址?

  URL url = new URL("link of url"); 
    BufferedInputStream input = new BufferedInputStream(url.openStream()); 
     PDFParser parsepdf = new PDFParser(input); 
     parsepdf.parse(); 
     textofpdf = new PDFTextStripper().getText(parsepdf.getPDDocument()) 

所以基本上我有一個Selenium Test類,它執行登錄和打開報告的功能。之後,我打電話給上面的PDFParse方法。此方法當前的功能就好像加載了新的URL,而不是繼續當前的Selenium會話。

+0

登錄,您使用的是相同的瀏覽器訪問URL PDF或打開一個新的瀏覽器後? – LINGS

+0

在同一瀏覽器中,它會自動在不同的選項卡中打開。 –

回答

0

塊引用

試試這個:

PDFTextStripper pdfStripper = null; 
    PDDocument pdDoc = null; 
    COSDocument cosDoc = null; 
    String parsedText = null; 

     URL url = new URL(strURL); 
     Proxy proxy = new Proxy(Proxy.Type.HTTP, new   InetSocketAddress("172.18.65.50", 8080)); 
     URLConnection urlc = url.openConnection(proxy); 
     urlc.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0a2) Gecko/20110613 Firefox/6.0a2"); 
     RandomAccessBufferedFileInputStream file = new RandomAccessBufferedFileInputStream(urlc.getInputStream()); 
     PDFParser parser = new PDFParser(file);   
     parser.parse(); 
     cosDoc = parser.getDocument(); 
     pdfStripper = new PDFTextStripper(); 
     pdfStripper.setStartPage(1); 
     pdfStripper.setEndPage(1); 

     pdDoc = new PDDocument(cosDoc); 
     parsedText = pdfStripper.getText(pdDoc);