
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Using XPATH to search text containing  
We can use the locator xpath to identify elements having search text with or spaces. Let us first examine the html code of a web element having trailing and leading spaces. In the below image, the text JAVA BASICS with tagname strong has spaces as reflected in the html code.
If an element has spaces in its text or in the value of any attribute, then to create an xpath for such an element we have to use the normalize-space function. It removes all the trailing and leading spaces from the string. It also removes every new tab or lines existing within the string.
Syntax
//tagname[normalize-space(@attribute/ function) = 'value']
For the web element JAVA BASICS appearing on the page, let us create an xpath// strong[text()='JAVA BASICS'] (without considering the spaces in the text). If we validate it in the Console with the expression - $x("//strong[text()='JAVA BASICS']"), we shall find there is no matching element(identified with length – 0).
Now, let us create an xpath expression using the normalize-space function. The xpath expression should be - //strong[normalize-space(text())='JAVA BASICS'].
Output
If we validate it in the Console with the expression - $x("//strong[normalizespace( text())='JAVA BASICS']"), we shall find there is a matching element(identified with length – 1).
On hovering on the result obtained, we shall find the text JAVA BASICS getting highlighted on the page.
- Related Articles
- Search text containing a new line in MySQL?
- How to use text() in xpath in Selenium with python?
- Using a regex with text search in MongoDB
- How to Create a Comment Box with a Containing Text Using CSS
- How to search and replace text in a file using Python?
- Angle $\angle ABC$ marked in the figure isa) acute angle (b) obtuse angle (c) reflex angle (d) none of these"\n
- 5 Best CLI Tools to Search Plain-Text Data Using Regular Expressions
- Search for PHP array element containing string?
- Common Linux Text Search
- Adding default search text to search box in HTML with JavaScript?
- How to identify the nth sub element using xpath?
- Implement Text search in MongoDB
- Perform MongoDB full text search
- How to find an element using the “XPath” in Selenium?
- How to Search and Replace text in Python?
