Let's say I have the following HTML code:
<a href="/site/somesite/">
somesite</a>
My question is how can I write an XPath expression that must use the text()
property to match the somesite
link and I cannot change the source?
The XPath text() function is a built-in function of selenium webdriver which is used to locate elements based on text of a web element. It helps to find the exact text elements and it locates the elements within the set of text nodes. The elements to be located should be in string form.
The '*' is used for selecting all the element nodes descending from the current node with @id-attribute-value equal to 'Passwd'.
enter image description here The XPath text() function locates elements within a text node while dot (.) locate elements inside or outside a text node. In the image description screenshot, the XPath text() function will only locate Success in DOM Example 2.
XPath is a major element in the XSLT standard. XPath can be used to navigate through elements and attributes in an XML document. XPath stands for XML Path Language. XPath uses "path like" syntax to identify and navigate nodes in an XML document. XPath contains over 200 built-in functions.
I'm not sure whether you want to lookup the URL based on the link text, or the link text based on the URL. This will get you the URL:
//a[normalize-space() = 'somesite']/@href
This will get you the text:
normalize-space(//a[@href = '/site/somesite/'])
Use normalize-space()
, which will throw away the leading and trailing whitespace characters(and condense repeating spaces in the middle of the text into a single space), so that you can compare the normalized text()
and use to filter in a predicate.
a[normalize-space(text())='somesite']
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