- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract text from a web page using Selenium and save it as a text file?
We can extract text from a webpage using Selenium webdriver and save it as a text file using the getText method. It can extract the text for an element which is displayed (and not hidden by CSS).
We have to locate the element on the page using any of the locators like id, class, name, xpath, css, tag name, link text or partial link text. Once the text is obtained, we shall write its content to a file with the help of File class.
Let us obtain the text – You are browsing the best resource for Online Education from the below page −
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import java.nio.charset.Charset; public class GetTxtSaveFile{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://www.tutorialspoint.com/index.htm"); // identify element WebElement e = driver.findElement(By.tagName("h4")); //obtain text String s = e.getText(); //write text to file File f = new File("savetxt.txt"); try{ FileUtils.writeStringToFile(f, s, Charset.defaultCharset()); }catch(IOException exc){ exc.printStackTrace(); } driver.quit(); } }
Output
The savetxt.txt file gets generated within the project which captures the text from the page.
- Related Articles
- How to write text and output it as a text file using R?
- Save a Web Page with Python Selenium
- How to extract text from a Javascript alert in Selenium with python?
- How to Create and Save text file in JavaScript?
- How to extract the text of a webelement in Selenium?
- How To Extract Text From A HTML Tag In Text Format?
- How to get the text from a website using selenium?
- Get text using selenium web driver in python?
- How to read a text file in Selenium with python?
- How to write a text file in Selenium with python?
- Python Program to extract email-id from URL text file
- Smart Ways to Save a Web Page Forever!!
- How to upload a file in Selenium with no text box?
- How to extract numbers from text using Python regular expression?
- How to extract date from text using Python regular expression?

Advertisements