Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath text() expression that contains a new line

Tags:

xpath

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?

like image 544
Silviu Avatar asked Dec 02 '10 20:12

Silviu


People also ask

What is text () function 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.

Why* is used in XPath?

The '*' is used for selecting all the element nodes descending from the current node with @id-attribute-value equal to 'Passwd'.

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. In the image description screenshot, the XPath text() function will only locate Success in DOM Example 2.

What is XPath of an element?

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.


2 Answers

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/'])
like image 199
John Kugelman Avatar answered Jan 04 '23 12:01

John Kugelman


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']
like image 39
Mads Hansen Avatar answered Jan 04 '23 13:01

Mads Hansen