XPath select second element of many unless only one exists

Viewed 362

I have a webpage with three <input> elements that all have the same name attribute. Ideally, I would like to select the second of these elements except sometimes there is only one element on the page and I want to instead select that element.

Ideally I would like something like (pseudo-code since max doesn't exist)

(//input[@name='myname'])[max(1, last()-1)]

I thought that maybe I could do something like the following except it yields all three elements

(//input[@name='myname'])[last()-1 or 1]

What is the best way to accomplish this using XPath?

1 Answers
Related