- 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
Finding text on page with Selenium 2
We can find text on page with Selenium webdriver. First of all, we shall identify the element with the help of the locator xpath. In xpath, we can use the contains() and text() functions.
Let us find the below highlighted text on the page −
The xpath expression shall be //*[contains(text(),'You are browsing')]. To obtain the text of the element, the getText method is used. Let us check the xpath expression we have created from the Console tab with the expression: $x("//*[contains(text(),'You are browsing')]").
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 FindElmntsText{ 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/index.htm"); // identify element with text WebElement i= driver.findElement (By.xpath("//*[contains(text(), 'You are browsing')]")); // get text for element System.out.println("Text is: " + i.getText()); } driver.close(); }
Output
- Related Articles
- How to identify elements based on text visible on page in Selenium?
- What to press ctrl +f on a page in Selenium with python?
- What to press ctrl +c on a page in Selenium with python?
- Save a Web Page with Python Selenium
- How to perform scrolling action on page in Selenium?
- Remove any text not inside element tag on a web page with JavaScript?
- Get page title with Selenium WebDriver using Java.
- Take screenshot of full page with Selenium Python with chromedriver.
- How to extract text from a web page using Selenium and save it as a text file?
- Clear text from textarea with selenium.
- JavaScript code to de-select text on HTML page.
- Highlight a text, every time page loads with JavaScript
- Wait until page is loaded with Selenium WebDriver for Python.
- Wait for complex page with JavaScript to load using Selenium.
- How can I check if some text exist or not in the page using Selenium?

Advertisements