This is the HTML code:
<div> <span></span> Elangovan </div>
I want to write an XPath for the div
based on its contained text. I tried
//div[contains(text(),'Elangovan')]
but this is not working.
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.
The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]
Using XPath- text() method, we can write the Java code along with the dynamic XPath location as: findElement(By. xpath("//*[text()='Google offered in')]"));
Replace text()
with string()
:
//div[contains(string(), "Elangovan")]
Or, you can check that span
's following text sibling contains the text:
//div[contains(span/following-sibling::text(), "Elangovan")]
Also see:
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