I have a node as follows:
<span class="portal-text-medium">Office Hours</span>
For the XPath I use
//span[text()='Office Hours']
which should work, but it never does. I can use *contains(text(),'Office Hours')]*
but that won't find an exact match and I have to verify there is no "*". This is not the only time it hasn't worked for me. I have seen it work before so I don't know what is wrong. Any idea?
Yes I can, and do, use starts-with
but it is not quite the same.
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.
Xpath Node is defined as a point where the path address initiates, as it follows a concept of nodes. In simple terms they are the individual elements of the Xpath hierarchical structure which are termed as a node and enable an XSL processing. Xpath expressions could be done with HTML and XML.
enter image description here The XPath text() function locates elements within a text node while dot (.) locate elements inside or outside a text node.
text(): A built-in method in Selenium WebDriver that is used with XPath locator to locate an element based on its exact text value. contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on partial text match.
text() =
is different than XPath . =
(Matching text nodes is different than matching string values)
The following XPaths are not the same...
//span[text() = 'Office Hours']
Says:
Select the span
elements that
have an immediate child text node equal to 'Office Hours`.
//span[. = 'Office Hours']
Says:
Select the span
elements whose string value is equal to 'Office Hours`.
In short, for element nodes:
The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order.
The following span
elements would match only #1:
<span class="portal-text-medium">Office Hours<br/>8:00-10:00</span>
<span class="portal-text-medium">My<br/>Office Hours</span>
The following span
elements would match only #2:
<span class="portal-text-medium"><b>Office</b> Hours</span>
<span class="portal-text-medium"><b><i>Office Hours</i></b></span>
The following span
element would match both #1 and #2:
<span class="portal-text-medium">Office Hours</span>
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