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

0 votes
Can anyone please suggest how can I extract data from a web page and save it to a text/doc file using Selenium?
May 7, 2019 in Selenium by Isha
33,103 views

1 answer to this question.

0 votes

Hello Isha, you can checkout this code snippet for extracting text from a web element and saving it as a text or doc 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();
 }

}

For further understanding, you can refer to the Selenium Course.

answered May 7, 2019 by Anvi
• 14,150 points
writeStringToFile( 
this code is deprated in latest java /Selenium 

Related Questions In Selenium

+1 vote
1 answer
0 votes
1 answer

How to download a pdf file diagrammatically from a web page with .html extension?

For downloading a pdf file: import java.io.File; import java.io.FileOutputStream; import ...READ MORE

answered Sep 28, 2018 in Selenium by Meci Matt
• 9,460 points
774 views
0 votes
2 answers

How to get the text from a website using selenium?

driver.findElement(By.cssSelector("p")).getText() or  IWebElement element = Browser.GetElementByCssSelector("div.loginbox p"); string text = ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
25,076 views
0 votes
1 answer

How can I locate web elements from a web page using Selenium Webdriver?

Hey @Dushyant, Locating elements in WebDriver is ...READ MORE

answered May 29, 2019 in Selenium by Shreya
2,597 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,617 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,572 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,629 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,519 views
0 votes
2 answers

How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver ?

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy("X", " y ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
5,686 views
0 votes
1 answer

How to mouse hover on a web element using Selenium WebDriver?

Hello Nitin, to automate mouse hovering over ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,150 points
6,175 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP