Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get parent element name in XPath

Tags:

dom

xpath

I am trying to figure out how to get the name of the parent from a text node's scope.

//text()[name(parent)='p']

How can you get the name of the current node's parent?

like image 358
Xeoncross Avatar asked Feb 10 '12 22:02

Xeoncross


People also ask

How do you get the parent of an element in XPath?

A Parent of a context node is selected Flat element. A string of elements is normally separated by a slash in an XPath statement. You can pick the parent element by inserting two periods “..” where an element would typically be. The parent of the element to the left of the double period will be selected.

How do you find the parent of a element in selenium?

Also we can identify the parent element of a known element with the help of Javascript Executor. We have to pass the Javascript command return arguments[0]. parentNode and the webelement as parameters to the method executeScript. The parentNode command in Javascript is used to point to the parent of an element.

How do I traverse back to parent in XPath?

IN the XPath we can traverse backward using the double dots(..). We can traverse backward as many levels as we want like ../../… For example, if we want to find the parent of any child div selector then we can use it as.


1 Answers

If you're trying to test the name, you almost had it:

//text()[name(parent::*)='p']

If you're trying to return the name:

name(//text()/parent::*)
like image 105
Daniel Haley Avatar answered Oct 08 '22 02:10

Daniel Haley