How can I create an XPath locator for this (duplicate) element?

2,267 views
Skip to first unread message

Mike Riley

unread,
Mar 15, 2012, 8:52:45 PM3/15/12
to Selenium Users
I am trying to navigate drop down menus and the web site developers
put identical strings for two menu items that I need to be able to
select between.

They look something like this:

<div style="blah blah blah">View Transactions</div>

Because the menus are dynamic based on various settings at the time, I
can't use the //div[13] like the IDE suggests (with no alternate
suggestion, BTW).

This path would work to get at the first one:
//div[text()='View Transactions']

Is there some variation where I can say go to the second one? I tried
putting [2] in all the places that made sense to me and none of them
worked. I am hoping someone can suggest a locator that will work. An
absolute path won't work because of the dynamic nature of the menus.

Mike

Ashok Tulachan

unread,
Mar 15, 2012, 9:20:11 PM3/15/12
to seleniu...@googlegroups.com
Hi Mike
I am sure the label might not be identical so try this
selenium.select("//div["label=View Transactions"]);


Mike

--
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.




--
Regards,
 
Ashok Tulachan
 

Luke Inman-Semerau

unread,
Mar 15, 2012, 10:33:14 PM3/15/12
to seleniu...@googlegroups.com
I believe you can do this:

(//div[.='View Transactions'])[2]

Going off memory and didn't try that myself though ;)

-Luke

Oliver

unread,
Mar 16, 2012, 2:52:21 AM3/16/12
to Selenium Users
Thank Luke

Vishal Mishra

unread,
Mar 16, 2012, 7:43:03 AM3/16/12
to Selenium Users
(//div[@style='View Transactions'])[<pass the count>]

Mike Riley

unread,
Mar 16, 2012, 3:30:52 PM3/16/12
to Selenium Users
Hi Ashok,

Given:

<div style="blah blah blah">View Transactions</div>
...
<div style="blah blah blah">View Transactions</div>

How would that be able to distinguish between the two?

I haven't used label before, but I am sure that would need to be
either label()='' or @label='' and I tried both ways and it did not
work. It didn't find either of those elements, using the IDE to test
it.

Mike

Mike Riley

unread,
Mar 16, 2012, 3:35:03 PM3/16/12
to Selenium Users
Thanks for the idea Luke and Vishal.

As with what Ashok posted, I tried several variations of this using
the IDE, but when clicking the Find button it says it could not find
the locator. Here is what I tried:

(//div[text()='View Transactions'])[2]
//(div[text()='View Transactions'])[2]
//div([text()='View Transactions'])[2]

I was not aware you could use parentheses in an XPath like this. Now
maybe my using the IDE, which would be using RC methods, is at fault
here. Is it possible that WebDriver would be able to handle this, but
RC would not?

Mike

Ashok Tulachan

unread,
Mar 16, 2012, 5:00:48 PM3/16/12
to seleniu...@googlegroups.com
Hi Mike
 
I thought that the label was different for every div. Anyway is 'id' for the element same as well?
 
If it is, that is very weird and difficult to automate as you can't do loop as well. 

--
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.




--
Regards,
 
Ashok Tulachan
 

Mike Riley

unread,
Mar 16, 2012, 7:14:37 PM3/16/12
to Selenium Users
Ashok,

There is no ID or NAME property for any of the menu items (don't get
me started!), so I can't use that. I was hoping someone would have an
idea for an XPath variation that would work for this. If it comes to
it I will ask for them to put one on it, but if I do I am going to ask
for it on all menu items and who knows when they will get it done.
That is why I was looking for a way to make it work with XPath.

Thanks.

Mike
Reply all
Reply to author
Forward
0 new messages