- 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 text from Selenium element WebElement object?
We can get text from a webelement with Selenium webdriver. The getText() methods obtains the innerText of an element. It fetches the text of an element which is visible along with its sub elements. It ignores the trailing and leading spaces.
First of all we need to identify the element with help of any of the locators like id, class, name, xpath or css and then apply getText() method on it to get the text content of the element.
Let us get the text of the element About Careers at Tutorials on the page−
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class GetElementText{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element WebElement p=driver.findElement(By.xpath("//h1")); //getText() to obtain text String s= p.getText(); System.out.println("Text content is : " + s); driver.close(); } }
Output
- Related Articles
- How to extract the text of a webelement in Selenium?
- How to get HTML code of a WebElement in Selenium?
- How to get attribute of element from Selenium?
- How to fetch values from a webelement in Selenium with python?
- How to get entered text from a textbox in selenium?
- How to get the text from a website using selenium?
- Get HTML Source of WebElement in Selenium WebDriver using Python.
- How to get typed text from a textbox by using Selenium Webdriver?
- How to get text found between span – Selenium
- How to get Tooltip Text in Selenium Webdriver?
- How to get text from each cell of an HTML table using Selenium?
- How to get text with selenium web driver in python?
- How to find an element using the “Link Text/Partial Link Text” in Selenium?
- How can I consistently remove the default text from an input element with Selenium?
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?

Advertisements