Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPATH: Find piece of text anywhere in document

Tags:

Is there any way to just search the whole html document for a piece of text without worrying about tags, classes etc?

like image 737
user3303266 Avatar asked May 16 '17 08:05

user3303266


People also ask

How do I find a specific text in XPath?

So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));

Can we use text () in XPath?

Using XPath- text() method, we can write the Java code along with the dynamic XPath location as: findElement(By. xpath("//*[text()='Google offered in')]"));

What is text () function in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.

How do I navigate to parent node in XPath?

Hey Hemant, we can use the double dot (“..”) to access the parent of any node using the XPath. For example – The locator //span[@id=”first”]/.. will return the parent of the span element matching id value as 'first.


1 Answers

Yes, something like this :

//text()[contains(.,'keyword')]

Or, use one of the following XPath if you prefer to return parent element where the target keyword resides :

//*[text()[contains(.,'keyword')]]
//text()[contains(.,'keyword')]/..
like image 143
har07 Avatar answered Nov 15 '22 13:11

har07