1

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.

smac89
  • 39,374
  • 15
  • 132
  • 179
Fawaz Ahmed
  • 1,082
  • 2
  • 14
  • 18

1 Answers1

1

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!!

Paras
  • 3,197
  • 2
  • 20
  • 30