- 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 get the text from a website using selenium?
We can get the text from a website using Selenium webdriver USING the getText method. It helps to obtain the text for a particular element which is visible or the inner text (which is not concealed from the page).
First of all, we have to identify the element on the page for which we want to get the text with the help of any locators like id, class, name, xpath, css, tag name, link text or partial link text.
Let us try to retrieve the text - ENJOY PREMIUM CONTENT AT AFFORDABLE PRICE from the below page −
Syntax
WebElement n =driver.findElement(By.tagName("h2")); String s = n.getText();
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; public class ElementTxt{ 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 with tagname WebElement s = driver.findElement(By.tagName("h2")); // obtain element text String st = s.getText(); System.out.println("Text is : " + st); driver.quit(); } }
Output
- Related Articles
- How to get typed text from a textbox by using Selenium Webdriver?
- How to get entered text from a textbox in selenium?
- How to get text from Selenium element WebElement object?
- How to get text from each cell of an HTML table using Selenium?
- How to get text found between span – Selenium
- How to get Tooltip Text in Selenium Webdriver?
- Get text using selenium web driver in python?
- How to extract text from a web page using Selenium and save it as a text file?
- How to get text with selenium web driver in python?
- How can I parse a website using Selenium and Beautifulsoup in python?
- How to input text in the text box without calling the sendKeys() using Selenium?
- How can I get the href of elements found by partial link text using Selenium?
- How to find an element using the “Link Text/Partial Link Text” in Selenium?
- How to capture the text from Alert Message in Selenium Webdriver?
- How to get website links using Invoke-WebRequest in PowerShell?

Advertisements