tricky xpath question?

166 views
Skip to first unread message

sbtang

unread,
Jan 3, 2011, 6:44:01 PM1/3/11
to Selenium Users
Hi folks, i ran into a pretty tricky xpath questions, assume you have
a page with two elements with identical attributes, like this

<div class='content'></div>
<div class='content'></div>
<div class='content'></div>
<div class='content'></div>

There are other divs on the page as well. Inside each div is a
specific link that i want to click. How do i specify using xpath like,
the second div[@class='content']? I tried stuff like

xpath=div[@class='content'][0] and that did not work.

Help.

Steve

NeelakandanRVJ

unread,
Jan 3, 2011, 11:54:47 PM1/3/11
to seleniu...@googlegroups.com
HI,
   Try to use position of that element you can fine it.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


santhosh soma

unread,
Jan 4, 2011, 12:26:13 AM1/4/11
to seleniu...@googlegroups.com
did u try the Dom Location like

Document.getelementsbyclassname(' content')[0]
Document.getelementsbyclassname(' content')[1]
Document.getelementsbyclassname(' content')[2]
Document.getelementsbyclassname(' content')[3]
Document.getelementsbyclassname(' content')[4]

let me know that if u r still unable to locate that element.

Thanks
Santhosh.

farheen khan

unread,
Jan 4, 2011, 1:37:38 AM1/4/11
to seleniu...@googlegroups.com
Hi,
 I you are using java, then this might work,
 selenium.click("//div[@class='content' and position()=2]");

Farheen
http://seleniumready.blogspot.com/



Niraj Kumar

unread,
Jan 4, 2011, 1:49:19 AM1/4/11
to seleniu...@googlegroups.com
http://automationtricks.blogspot.com/2010/09/how-to-locate-element-which-have-same.html

This may help you


On Tue, Jan 4, 2011 at 5:14 AM, sbtang <sbt...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.




--
Thanks & Regard,
Niraj Kumar

Steve Tangsombatvisit

unread,
Jan 4, 2011, 3:22:02 AM1/4/11
to seleniu...@googlegroups.com
thanks all, i will try these tomorrow and see how they work! These are all good suggestions. I'm relatively new to using xpath so some of these i have bever even heard of before.

Steve
---------------------------------------------------
Steve B Tang
My website - http://sbtang.blogspot.com/

sriharsha a

unread,
Jan 4, 2011, 3:51:36 AM1/4/11
to seleniu...@googlegroups.com
Or u can use last() function in xpath as well , this might work . Please Try.

selenium.click("//div[@class='content' and last()]");
selenium.click("//div[@class='content' and last()-1]");
......
Thanks,
Sriharsha
On Tue, Jan 4, 2011 at 10:24 AM, NeelakandanRVJ <neel...@gmail.com> wrote:

Steve Tangsombatvisit

unread,
Jan 4, 2011, 12:10:34 PM1/4/11
to seleniu...@googlegroups.com
hi friends, thanks for everyone's input.. For some reason, i still cannot get this working, i am using Ruby + Selenium, and here are some of the things i tried.

First of all, i did an get_xpath_count to assure myself that we are seeing multiple elements.  Also, i am using the focus command to test out which element is selected by which xpath expression. However, using some of the suggestions in this thread does not seem to work.. 

What i find is that if i use the "and last()" expression, it does not work, my code is like this
@selenium.focus("xpath=//div[@class='fj-menu-area forum-nav-control' and last()"])

So i also tried using the position() function, but that does not work to. This actually has some funny behavior,

@selenium.focus("xpath=//div[@class='fj-menu-area forum-nav-control' and position()=1"]) 
That above code works, as it focuses on the first instance of the element, HOWEVER, if i do

@selenium.focus("xpath=//div[@class='fj-menu-area forum-nav-control' and position()=2"]) 
This code says invalid xpath expression, even though when i do an xpath_count on
xpath=//div[@class='fj-menu-area forum-nav-control'] i get multiple instances..

What do you guys think? Any ideas? Thanks!

Steve

David Rosskopf

unread,
Jan 4, 2011, 12:31:31 PM1/4/11
to Selenium Users
Did you try starting at 1 instead of 0?

div[@class='content'][1]

I found 0 didn't work, but starting at 1 did.

Steve Tangsombatvisit

unread,
Jan 4, 2011, 12:44:48 PM1/4/11
to seleniu...@googlegroups.com
HI David, yeah i tried starting at 1. 1 works, but if i use 2, 2 does not work, even though there are two elements on the page.

Steve

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

Ross Patterson

unread,
Jan 4, 2011, 1:07:48 PM1/4/11
to seleniu...@googlegroups.com

I suspect your HTML is more complex than the example you’ve shown us –in particular, that the identical <DIV>s are not contained within the same parent element.  That would explain your problems.  XPath’s position() function (and its shorthand form “[n]”) doesn’t do what most folks think it does.  It selects elements that are at the specified position relative to their parent element, not relative to the page or to some deeper ancestor element.  Thus “div[@class=’content’][2]” means “the div that has the CSS class ‘content’ and that is the second node within its enclosing node”, which is only true when the <DIV>s are all in the same container (as in your example).

 

If you really want the n’th element matching some XPath expression, you need to use “xpath=(...the expression...)[n]”, which means “the n’th node in the set of nodes selected by ‘...the expression...’”.  For example, “xpath=(//div[@class=’content’])[2]”.  Note the use of the parentheses – they’re critical.

 

Ross

Steve Tangsombatvisit

unread,
Jan 4, 2011, 1:49:59 PM1/4/11
to seleniu...@googlegroups.com
hi Ross, thanks you nailed it. My divs were not all on the same container level. If they were, then using the convention described above works just fine. Thanks!

Steve
Reply all
Reply to author
Forward
0 new messages