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 in Selenium by Isha
29 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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();
 }

}
answered May 7 by Anvi
• 5,880 points

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,400 points
54 views
0 votes
1 answer

How to get the text from a website using selenium?

hey, you can use get.text() method to ...READ MORE

answered Dec 31, 2018 in Selenium by Sudhanshu
65 views
0 votes
1 answer
0 votes
1 answer

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,030 points
61 views
0 votes
1 answer
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,130 points
432 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
49 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 in Selenium by Anvi
• 5,880 points
14 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.