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


Can anyone please suggest how can I extract data from a web page and save it to a text/doc file using Selenium?



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 course page for more information, where you can find the selenium tutorials and selenium frequently asked interview questions and answers as well.


This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.
Protected by Astra Security