Is there any way to just search the whole html document for a piece of text without worrying about tags, classes etc?
So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));
Using XPath- text() method, we can write the Java code along with the dynamic XPath location as: findElement(By. xpath("//*[text()='Google offered in')]"));
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.
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.
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')]/..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With