Thursday, October 27, 2011

Select one of many similar elements by index in Selenium?

Download Select one of many similar elements by index in Selenium?, Selenium Notes, Videos, examples by.www.laxmiroy.blogspot.com/


Imagine there are 10 web buttons on the page. All have the id buttonfollowed by a dynamic number. So the buttons would have id button12234, button12235,button12236 and so on. Now if you wish to select the second button from the top of the page.
Easy to this in QTP.Browser().Page().WebButton().Click("htmlid:=button.*","index:=1")should do the job.
In selenium use the following
xpath=(//input[contains(@id,'button')])[2]

The prefix xpath= is essential or this expression won't work.
Select one of many similar elements by index in Selenium?
Imagine there are 10 web buttons on the page. All have the id buttonfollowed by a dynamic number. So the buttons would have id button12234, button12235,button12236 and so on. Now if you wish to select the second button from the top of the page.
Easy to this in QTP.Browser().Page().WebButton().Click("htmlid:=button.*","index:=1")should do the job.
In selenium use the following
xpath=(//input[contains(@id,'button')])[2]

The prefix xpath= is essential or this expression won't work.
Way to get XPATH in Internet Explorer
Are you stuck with an application that only works in IE?
Do you want the XPATH of an element that can be seen only IE? Unable to replicate the scenario in firefox. There are many firefox addons like xpather, xpath-checker and firebug that will give you the xpath of an element in a second. But sadly there is no addon or tool avaialable that will do this for IE. For most cases you can get the xpath of the elements that fall in your script using the above tools in firefox and tweak them a little (if required) to make them work in IE. But if you are testing an application that will work only in IE or the specific scenario or page that has this element will open-up/play-out only in IE then you cannot use any of the above mentione tools to find the XPATH.
Well the only thing that works in this case is the Bookmarklets that were coded just for this purpose. Bookmarklets are JavaScript code that you will add in IE as bookmarks and later use to get the XPATH of the element you desire. Using these you can get the XPATH as easily as you get using xpather or any other firefox addon.

No comments:

Post a Comment