Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath Multiple Predicates

Tags:

xpath

I am trying to quickly find a specific node using XPath but it seems my multiple predicates are not working. The div I need has a specific class, but there are 3 others that have it. I want to select the fourth one so I did the following:

//div[@class='myCLass' and 4]

However the "4" is being ignored. Any help? I am new to XPath.

Thanks.

like image 934
TheGateKeeper Avatar asked Jan 20 '26 00:01

TheGateKeeper


2 Answers

If a xpath query returns a node set you can always use the [OFFSET] operator to access a certain element of it.

Use the following query to access the fourth element that matches the @class='myClass' predicate:

//div[@class='myCLass'][4]

@WilliamNarmontas answer might be an alternative to the syntax showed above.

like image 152
hek2mgl Avatar answered Jan 22 '26 21:01

hek2mgl


Alternatively,

//div[@class='myCLass' and position()=4]
like image 39
ScalaWilliam Avatar answered Jan 22 '26 20:01

ScalaWilliam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!