Home  >  Article  >  Java  >  Use XPATH to search for text containing  

Use XPATH to search for text containing  

WBOY
WBOYforward
2023-09-10 11:33:011194browse

We can use locator xpath to identify elements with search text

With or spaces. Let us first examine the html code of a web element Trailing and leading spaces. In the image below, the text JAVA BASICS has The tagname Strong contains spaces, as reflected in the html code.

Use XPATH to search for text containing  

Created if there are spaces in the element's text or in the value of any attribute For an xpath of such an element, we have to use the normalized spatial function. it Removes all trailing and leading spaces from a string. It also removes every A new label or an existing line in the string.

Syntax

//tagname[normalize-space(@attribute/ function) = 'value']

For Web elements JAVA BASICS that appear on the page, let us create an xpath// strong[text()='JAVA BASICS'] (spaces in the text are not considered). if we Use the expression $x("//strong[text()='JAVA' in the console to verify BASICS']"), we will find that there is no matching element (identified by having a length of 0).

Use XPATH to search for text containing  

Now, let us use the normalize-space function to create an xpath expression The xpath expression should be - //strong[normalize-space(text())='JAVA BASICS'].

Output

Use XPATH to search for text containing  

If we are in Verify it using expression in console - $x("//strong[Normalized space( text())='JAVA BASICS']"), we will find a matching Element (identified by length - 1).

Hover the mouse over the obtained results and we will find the text JAVA BASICS getting Highlight on the page.

The above is the detailed content of Use XPATH to search for text containing  . For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete