Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing text() nodes vs string values in XPath

Tags:

html

xml

xpath

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.

like image 415
Tony Avatar asked Jan 04 '16 14:01

Tony


People also ask

What does text () do in XPath?

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.

What does node () do in XPath?

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.

What is the difference between DOT and text in XPath?

enter image description here The XPath text() function locates elements within a text node while dot (.) locate elements inside or outside a text node.

How do I search for text in XPath?

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.


1 Answers

XPath text() = is different than XPath . =

(Matching text nodes is different than matching string values)

The following XPaths are not the same...

  1. //span[text() = 'Office Hours']

    Says:

    Select the span elements that have an immediate child text node equal to 'Office Hours`.

  2. //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.

Examples

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>
like image 190
kjhughes Avatar answered Oct 10 '22 12:10

kjhughes