Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In XPath how to select the element content

Tags:

element

xpath

Is there a way of writing an XPath expression to select the content of the element.

e.g.

<Element>xxx</Element> 

Assuming I can write XPath (/Element) to get Element how do I tweak the XPath to get xxxx returned rather than the Element wrapper?

EDIT/ANSWER

To do this in dom4j world use the Element.valueOf(String xpathExpression) rather than the .selectXXX() methods.

like image 541
Mike Q Avatar asked Mar 18 '10 13:03

Mike Q


People also ask

How do I select text in XPath?

Do note that /html/text() doesn't select all text nodes in the document -- only the text nodes that are children (not descendents) of the top, html element. You probably want /html//text() . Some knowledge and understanding of XPath is typically required in order to construct XPath expressions.

What is text () in XPath?

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.

How do I use XPath to find elements?

We can find an element using the xpath locator with Selenium webdriver. To identify the element with xpath, the expression should be //tagname[@attribute='value']. To identify the element with xpath, the expression should be //tagname[@class='value']. There can be two types of xpath – relative and absolute.

Is used to select in XPath?

In XPath, path expression is used to select nodes or node-sets in an XML document. The node is selected by following a path or steps.


2 Answers

Use the value-of element:

<xsl:value-of select="/Some/Path/To/Element"/> 

If you can only specify an XPath then use the text function like this:

/Some/Path/To/Element/text()

like image 190
Andrew Hare Avatar answered Sep 20 '22 10:09

Andrew Hare


A bit too late but...

data(Element) 

...should also be fine.

like image 29
g2mk Avatar answered Sep 21 '22 10:09

g2mk