How to extract text from a web page using selenium and save it as a text file?

in selenium
selenium Tutorials
1 Answers to this question

The below code snippet will help in extracting text from a webpage using Selenium and saving it as a text file.




import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;


public class ExtractText {

 static WebDriver driver;


 public static void main(String[] args) throws IOException {

  System.setProperty("", "chrome_driver_path");

  driver = new ChromeDriver();




  String output = driver.findElement(By.xpath("/html/body/div[1]/div[5]/div/div/div[1]/div[2]/div[1]/div")).getText();

  File DestFile= new File("extractedFilePath");

  FileUtils.writeStringToFile(DestFile, output);




If you want to unleash your potential in this competitive field, please visit the Selenium Certification Training page for more information, where you can find the    Selenium Tutorial    and    Selenium Interview Questions FAQ's and answers    as well.

For more updates on the latest courses stay tuned to HKR Trainings.

To Top