Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath query to get nth instance of an element

Tags:

xml

xpath

People also ask

How do you find the nth element using XPath?

By adding square brackets with index. By using position () method in xpath.

How do I find the nth element in selenium?

You can use “tag:nth-of-type(n)”. It will select the nth tag element of the list.

How do I select the first child in XPath?

The key part of this XPath is *[1] , which will select the node value of the first child of Department .

What does * indicate in XPath?

The XPath default axis is child , so your predicate [*/android.widget.TextView[@androidXtext='Microwaves']] is equivalent to [child::*/child::android.widget.TextView[@androidXtext='Microwaves']] This predicate will select nodes with a android. widget. TextView grandchild and the specified attribute.


This is a FAQ:

//somexpression[$N]

means "Find every node selected by //somexpression that is the $Nth child of its parent".

What you want is:

(//input[@id="search_query"])[2]

Remember: The [] operator has higher precedence (priority) than the // abbreviation.


This seems to work:

/descendant::input[@id="search_query"][2]

I go this from "XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition" by Michael Kay.

There is also a note in the "Abbreviated Syntax" section of the XML Path Language specification http://www.w3.org/TR/xpath/#path-abbrev that provided a clue.