Choosing Nth XPath match ?

712 views
Skip to first unread message

Arthur PETRY

unread,
Nov 15, 2007, 11:24:13 AM11/15/07
to selenium-users...@googlegroups.com
Hi all,

I'm have a XPath that match a list of elements.

I can have the nb of elements using getXpathCount.

But I don't know how to reach the nth element in selenium.

I'm beginning with XPath, but it seems to me that there's not always a xpath corresponding to a nth XPath match.

my example :
"//div[@id='content_list']//div[@class='content_item']" return 10 elements how can I match the second one ?

and no "//div[@id='content_list']//div[@class='content_item'][2]" or "//div[@id='content_list']//div[@class='content_item' and position()=2]" are not working.

I think it's a recurrent problem seing this 2 others threads :
http://forums.openqa.org/thread.jspa?threadID=5023
http://forums.openqa.org/thread.jspa?messageID=28958&#28958

I've digged into selenium-browserbot.js, _findElementUsingFullXPath is returning "xpathResult.value[0]". I was wondering if we can try xpathResult.value[n]
But after looking in xpath.js, I'm not sure it's that simple...

Any Idea ?

Arthur PETRY

unread,
Nov 15, 2007, 11:27:28 AM11/15/07
to selenium-users...@googlegroups.com
Hi all,

I'm have a XPath that match a list of elements.

I can have the nb of elements using getXpathCount.

But I don't know how to reach the nth element in selenium.

I'm beginning with XPath, but it seems to me that there's not always a xpath corresponding to a nth XPath match.

my example :
"//div[@id='content_list']//div[@class='content_item']" return 10 elements how can I match the second one ?

and no "//div[@id='content_list']//div[@class='content_item'][2]" or "//div[@id='content_list']//div[@class='content_item' and position()=2]" are not working.

I think it's a recurrent problem seing this 2 others threads :
http://forums.openqa.org/thread.jspa?threadID=5023
http://forums.openqa.org/thread.jspa?messageID=28958

Arthur PETRY

unread,
Nov 15, 2007, 11:28:32 AM11/15/07
to selenium-users...@googlegroups.com
Oups wrong forum.
Sorry
I've repost it in 'selenium users'
http://forums.openqa.org/thread.jspa?threadID=10977

Arthur PETRY

unread,
Nov 15, 2007, 2:20:31 PM11/15/07
to selenium-users...@googlegroups.com
> {quote:title=dave stewart wrote:}
> I'd love to see your application under test to see if I could make it work... have you tried this?
>
> (//div[@id='content_list']//div[@class='content_item'])[2]

Wow !!

This is working in Firefox/XPather

Great...

But it's not working in selenium...

Thanks for the correct xpath though !

I will see If I could send you an extract of html (the application is not public yet... )

Thomas

unread,
Nov 16, 2007, 6:27:36 AM11/16/07
to selenium-users...@googlegroups.com
(//div[@id='content_list']//div[@class='content_item'])[2]

Since your locator doesn't start with "//" anymore, you might need to explicitly tell Selenium it's a XPath locator. Have you tried "xpath=(//div...)[2]" ?!

Arthur PETRY

unread,
Nov 16, 2007, 8:35:04 AM11/16/07
to selenium-users...@googlegroups.com
> {quote:title=thomas_h wrote:}
> Since your locator doesn't start with "//" anymore, you might need to explicitly tell Selenium it's a XPath locator. Have you tried "xpath=(//div...)[2]" ?!

Of course !!!

Now I can do what I want with Selenium.

Thanks a lot, all.

Reply all
Reply to author
Forward
0 new messages