How to download pdf file from website in Selenium Webdriver using java.


 import java.io.BufferedInputStream;  
 import java.io.BufferedOutputStream;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.io.InputStream;  
 import java.io.OutputStream;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.util.ArrayList;  
 import java.util.List;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.htmlunit.HtmlUnitDriver;  
   
   
 public class pdffiledownloadfromwebsite {  
   
 public static void main(String[] args) throws MalformedURLException, IOException {  
  List<String> uniqurl = new ArrayList();  
   
 // Initialize Webdriver driver   
 WebDriver driver = new HtmlUnitDriver();  
 // Go to pdf page  
 driver.get("http://www.banglakitab.com/kitab.htm");  
 // get all page urls  
 List<WebElement> pdfurllist = driver.findElements(By.tagName("a"));  
   
 for (WebElement elemnet : pdfurllist) {  
 String pdfurl = elemnet.getAttribute("href").trim();  
 //check pdf url  
 if (pdfurl.contains(".pdf")) {  
 // check one file download one time  
 if (!(uniqurl.contains(pdfurl))) {  
  uniqurl.add(pdfurl);  
 //print file name and download url  
 System.out.println(filename(pdfurl) + " ---> " + pdfurl);  
 //download file  
 URL url = new URL(pdfurl);  
 InputStream in = new BufferedInputStream(url.openStream());  
 OutputStream out = new BufferedOutputStream(new FileOutputStream(filename(pdfurl)));  
   
 for (int i; (i = in.read()) != -1;) {  
 out.write(i);  
 }  
 in.close();  
 out.close();  
             
  }  
  }  
 }  
   
 // close driver   
 driver.quit();  
   
 }  
   
   // get file name. It depend on download url pattent.  
   private static String filename(String url) {  
     String filename = "";  
     String file[];  
   
     file = url.split("\\/");  
     if (file[file.length - 1].contains("-")) {  
       filename = file[file.length - 1].trim().replace("-", " ");  
     } else if (file[file.length - 1].contains("%20")) {  
       filename = file[file.length - 1].trim().replace("%20", " ");  
     } else {  
       filename = file[file.length - 1].trim();  
     }  
   
     return filename;  
   }  
   
 }  

7 comments:

  1. What i should do if I need to set directory where will be downloded pdf-files

    ReplyDelete
    Replies
    1. Default download file in project_home directory
      set directory if want
      OutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\Torrent download\\"+filename(pdfurl)));
      Files downloaded below directory:
      D:\Torrent download\

      Delete
    2. As-salamu' Alaykum Hiro Mia!

      While run this code I got the below error:


      Exception in thread "main" java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/javascript/host/Event
      at org.openqa.selenium.htmlunit.HtmlUnitDriver.resetKeyboardAndMouseState(HtmlUnitDriver.java:498)
      at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:494)
      at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:454)
      at org.openqa.selenium.htmlunit.HtmlUnitDriver.(HtmlUnitDriver.java:180)
      at com.dartsip.general.court.PdfFileDownloadFromWebsite.main(PdfFileDownloadFromWebsite.java:29)
      Caused by: java.lang.ClassNotFoundException: com.gargoylesoftware.htmlunit.javascript.host.Event
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      ... 5 more


      What to do? What is the solution do you suggest??

      Delete
    3. use WebDriver driver = new FirefoxDriver(); instead of WebDriver driver = new HtmlUnitDriver();

      Delete
    4. Hiro Mia: How To Pdf File From Website In Selenium Webdriver Using Java. >>>>> Download Now

      >>>>> Download Full

      Hiro Mia: How To Pdf File From Website In Selenium Webdriver Using Java. >>>>> Download LINK

      >>>>> Download Now

      Hiro Mia: How To Pdf File From Website In Selenium Webdriver Using Java. >>>>> Download Full

      >>>>> Download LINK yK

      Delete
  2. Thanks for your quick reply.

    I want to download pdf from website using HTMLUnit.
    How to do achieve that??

    ReplyDelete
  3. Hiro Mia: How To Pdf File From Website In Selenium Webdriver Using Java. >>>>> Download Now

    >>>>> Download Full

    Hiro Mia: How To Pdf File From Website In Selenium Webdriver Using Java. >>>>> Download LINK

    >>>>> Download Now

    Hiro Mia: How To Pdf File From Website In Selenium Webdriver Using Java. >>>>> Download Full

    >>>>> Download LINK 8S

    ReplyDelete