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

selenium

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

1
Answers

Replies

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



import java.io.File;

import java.io.IOException;


import org.apache.commons.io.FileUtils;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class ExtractText {


 static WebDriver driver;


 @SuppressWarnings("deprecation")

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


  System.setProperty("webdriver.chrome.driver", "chrome_driver_path");


  driver = new ChromeDriver();

  driver.manage().window().maximize();

  driver.get("https://www.edureka.co/devops");

  

  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);

  driver.close();

 }


}
 
 

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.