Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Nth element with XPath - when element[N] fails

There were already some discussion about the Nth element in XPath:

XPath query to get nth instance of an element

Get Nth child of a node using xpath

XPath and PHP: Parse from the nth instance of an element

I'm using Selenium and PHPUnit for funcional testing. My problem is that on a page with a large form the Nth selector is not working. I have 80 input fields (using Selenium's getXpathCount('//input') call to get the exact number) - and I would like to iterate through that 80 fields and type some text. What I've already tried and not working (N is the index variable):

//input[N]
//input[position()=N]
input[N]
(//input)[N]
(.//input)[N]
css=input:nth-of-type(N)

And all the mixtures of the ones above. It usually fires an Exception saying: element not found at N = 2. I tried several XPath tools for browsers, so as native extensions like XPathEvaluator in Chrome - almost all gave me proper results. So it seems Seleniums XPath parser works a bit different way.

What do you think? Do I miss a major thing?

Update: Here you are my solution: getAllFields(): http://release.seleniumhq.org/selenium-remote-control/0.9.0/doc/java/com/thoughtworks/selenium/DefaultSelenium.html#getAllFields() and then I iterate through.

It actually doesn't solve the original problem, so I'm still interested about the answer.

Thanks!

like image 675
itarato Avatar asked Dec 09 '22 05:12

itarato


1 Answers

(//input)[15] returns the 15th input element in the document

//input[15] returns every input element that is the 15th input child of its parent element.

So some of your expressions at least are incorrect. But they aren't all incorrect, so I don't know why it isn't working for you.

like image 63
Michael Kay Avatar answered Jan 01 '23 11:01

Michael Kay