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;
}
}
How to download pdf file from website in Selenium Webdriver using java.
Subscribe to:
Post Comments (Atom)
What i should do if I need to set directory where will be downloded pdf-files
ReplyDeleteDefault download file in project_home directory
Deleteset directory if want
OutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\Torrent download\\"+filename(pdfurl)));
Files downloaded below directory:
D:\Torrent download\
As-salamu' Alaykum Hiro Mia!
DeleteWhile 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??
use WebDriver driver = new FirefoxDriver(); instead of WebDriver driver = new HtmlUnitDriver();
DeleteHiro Mia: How To Pdf File From Website In Selenium Webdriver Using Java. >>>>> Download Now
Delete>>>>> 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
Thanks for your quick reply.
ReplyDeleteI want to download pdf from website using HTMLUnit.
How to do achieve that??
Hiro Mia: How To Pdf File From Website In Selenium Webdriver Using Java. >>>>> Download Now
ReplyDelete>>>>> 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