3

using an expression like (//div[@class='nav']//a)[5] to retrieve a specific element with Selenium (triggered through phpunit) never suceeds for some reason.

The Xpath is valid, using other Xpath expressions works fine, but once the Xpath contains brakets the Selenium server (2.0rc2) starts returning ERROR: Element (//div[@class='nav']//a)[5] not found. even it that element is present.

Is this a limitation of the PHP-Webdriver for Selenium, is there some kind of workaround (to get the nth element within a node-set)?

Cheers

pagid
  • 13,559
  • 11
  • 78
  • 104
  • 1
    possible duplicate of [Can't get nth node in Selenium](http://stackoverflow.com/questions/3369342/cant-get-nth-node-in-selenium) – edorian Jun 07 '11 at 11:55
  • Try `xpath=(//div[@class='nav']//a)[5]` instead. – hakre Jun 07 '11 at 11:59
  • finally `xpath=(//div[@class='nav']//a)[position()=5]` does the trick, not sure why [5] alone doesnt work - anyways thanks for the xpath= hint. – pagid Jun 07 '11 at 13:01
  • @pagid, paste your comment as an answer and accept it. Think about the others that search for a solution :) – Janis Veinbergs Jun 07 '11 at 14:00
  • //div[@class='nav']//a[5] Did you try this? – A.J Jun 08 '11 at 02:40
  • Also, as a point of comparison, have you tried the test/locator with PHPUnit's Selenium extension (i.e. Selenium RC) as opposed to PHP WebDriver. To see if Selenium RC or WebDriver better supposed this type of locator. – David Sep 30 '11 at 23:21
  • One other comment, while the XPath is valid, are you sure that XPath leads to your intended element on the page? I've found sometimes you have to tweak things for different browsers, though particularly more for CSS locators. For example, I always test the XPath in browser via Firebug/FirePath/Firefinder to verify it detects correct element before using in Selenium code. If it doesn't lead to right element, that may explain why you also get failure with Selenium test. – David Sep 30 '11 at 23:25

2 Answers2

1

From topic Can't get nth node in Selenium i see you can try prepending xpath= to your expression to get it work.

Community
  • 1
  • 1
Janis Veinbergs
  • 6,907
  • 5
  • 48
  • 78
1

This was the final solution: xpath=(//div[@class='nav']//a)[position()=5]

Not sure why [5] didn't work, might still be an issue within phpunit

Cheers

pagid
  • 13,559
  • 11
  • 78
  • 104
  • But is it really issue with PHPUnit/PHP? Could be issue with Selenium/WebDriver. Only way to find out is run exact command/test using Selenium/WebDriver with another language like Java, C#, Ruby, Perl and see if it fails there. I suspect more of Selenium issue, as I'm assuming PHP just passes the data along to Selenium to process, unless it unintendedly preprocesses the data. – David Sep 30 '11 at 23:19