Fawaz Ahmed
Fawaz Ahmed

Reputation: 1574

Save as pdf in selenium using Phantomjs in java

I want to perform some tasks using selenium in java

//java code
WebDriver driver = new PhantomJSDriver();
driver.get("http://www.example.com");
WebElement element = driver.findElement(By.name("q"));  
element.sendKeys("Guru99");                 
element.submit(); 

After Submitting the form I want to save the result page as pdf. How can i achieve this. I don't know anything about other programming languages,so please help using java code.

Upvotes: 1

Views: 1175

Answers (1)

Paras
Paras

Reputation: 3235

This is what you can do to achieve your result:-

Once you navigate to your result page after submitting the text you need to get the source of the page using:-

String htmlFileContent = driver.getPageSource();

Then you can create a file with this html content something like:-

File file = new File("index.html");
file.createNewFile();
PrintWriter pw = new PrintWriter("index.html", "UTF-8");
pw.print(htmlFileContent);

Once the content is written in HTML file. You can use iText Library or any other pdf converter library to convert your html into pdf

Hope it helps!!

Upvotes: 1

Related Questions