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?
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.
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.
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.
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::*)
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