0

I'm trying to write an xpath expression for a Selenium test. I'm a little confused as to how (if at all) I can pick a specific element of a return set. I have a basic xPath expression to select all the div elements with a certain CSS class

//div[@class='step-title']

When I used a Firefox xpath extension, this expression returns 7 results

enter image description here

What I want to do is select the first (or second, third, etc) element of this result set. However, my instincts for path syntax fail me, because the following

//div[@class='step-title'][2]

Returns all 7 matches again.

enter image description here

I've tried several variations, all with the same result.

enter image description here

The reason I want to select a specific element is to test a feature that re-orders something on a page — i.e. "Does the first div contain the text "Checkout Method". If selecting a specific element is impossible, alternate suggestions on how to write a selenium test to accomplish my end goal would be appreciated.

Alana Storm
  • 164,128
  • 91
  • 395
  • 599

1 Answers1

0

Self help desk strikes again. Per this Stack Overflow question, [] has a higher priority than //, so you need to wrap your clause in parenthesis.

(//div[@class='step-title'])[1]
Community
  • 1
  • 1
Alana Storm
  • 164,128
  • 91
  • 395
  • 599