Pages

Search This Blog

Tuesday, September 28, 2010

How to locate an element which have same name and same atrributes in selenium

Automation using selenium is a great experience. It provides many way to identif an object or element on the web page.
But sometime we face the problems of idenfying the objects on a page which have same attributes. When we get more than
one element which are same in attribute and name like multiple checkboxes with same name and same id. More than one button having
same name and ids. There are no way to distingues those element. In this case we have problem to instruct selenium to identify a perticular
object on a web page.
I am giving you a simple example . In the below html source there are 6 checkboxes are there having same type and same name.
It is really tough to select third or fifth.

<html>
<body>
<input type='checkbox' name='chk'>first
<br><input type='checkbox' name='chk'>second
<br><input type='checkbox' name='chk'>third
<br><input type='checkbox' name='chk'>forth
<br><input type='checkbox' name='chk'>fifth
<br><input type='checkbox' name='chk'>sixth
</body>
</html>


Thare are some function we can use in Xpath to identify the abject in above cases.
An XPath expression can return one of four basic XPath data types:

* String
* Number
* Boolean
* Node-set

XPath Type : Functions
Node set : last(), position(), count(), id(), local-name(), namespace-uri(), name()
String : string(), concat(), starts-with(), contains(), substring-before(), substring-after(), substring(), string-length(), normalize-space(), translate()
Boolean : boolean(), not(), true(), false(), lang()
Number : number(), sum(), floor(), ceiling(), round()

I will show you how we can use some of these above functions in xpath to identify the objects.

Node Set : last()


In the above html file there are six checkboxes and all are having same attributes (same type and name)
How we can select the last checkbox based on the position. We can use last() function to indentify the last object among all similar objects.
Below code will check or uncheck the last checkbox.

selenium.click("xpath=(//input[@type='checkbox'])[last()]");

How we can select the second last checkbox and third last checkbox. We can use last()- function to indentify the last object among all similar objects.
Below code will check or uncheck the second last checkbox and thrid last checkbox respectively.

selenium.click("xpath=(//input[@type='submit'])[last()-1]");
selenium.click("xpath=(//input[@type='submit'])[last()-2]");


Node Set : position()

If you want to select any object based on their position using xpath then you can use position() function in xpath.
You want to select second checkbox and forth checkbox then use below command

selenium.click("xpath=(//input[@type='checkbox'])[position()=2]");
selenium.click("xpath=(//input[@type='checkbox'])[position()=4]");

above code will select second and forth checkbox respectively.

String : starts-with()

Many web sites create dynamic element on their web pages where Ids of the elements gets generated dynamically.
Each time id gets generated differently. So to handle this situation we use some JavaScript functions.

XPath: //button[starts-with(@id, 'continue-')]  


Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
To demonstrate, the element can be located based on the ‘suggest’ class without having
to couple it with the ‘top’ and ‘business’ classes using the following

XPath: //input[contains(@class, 'suggest')].



Example: How to click on link on the page which has many links with same name and attributes.
Below is the example of your html which has 3 links with same name and same attributes


<html>
<body>
<a href="http://www.google.com" name="a1">Link</a>
<a href="http://www.yahoo.com" name="a1">Link</a>
<a href="http://www.gmail.com" name="a1">Link</a>
</body>
</html>



If you want to click on first link then use below command

<tr>
 <td>clickAndWait</td>
 <td>xpath=(//a[@name='a1'])[position()=1]</td>
 <td></td>
</tr>

If you want to click on second link then use below command

<tr>
 <td>clickAndWait</td>
 <td>xpath=(//a[@name='a1'])[position()=2]</td>
 <td></td>
</tr>

If you want to click on last link or third link then use below command

<tr>
 <td>clickAndWait</td>
 <td>xpath=(//a[@name='a1'])[last()]</td>
 <td></td>
</tr>

OR

<tr>
 <td>clickAndWait</td>
 <td>xpath=(//a[@name='a1'])[position()=3]</td>
 <td></td>
</tr>

Selenium RC.

Click on first link
selenium.click("xpath=(//a[@name='a1'])[position()=1]");
selenium.waitForPageToLoad("80000");
Click on second link
selenium.click("xpath=(//a[@name='a1'])[position()=2]");
selenium.waitForPageToLoad("80000");
Click on last link
selenium.click("xpath=(//a[@name='a1'])[last()]");
selenium.waitForPageToLoad("80000");
Click on thrid link
selenium.click("xpath=(//a[@name='a1'])[position()=3]");
selenium.waitForPageToLoad("80000");

For rest of the function please keep reading my blogs i will be posting very soon.


61 comments:

  1. Thank you so much for the easy to read and detailed explantion!

    ReplyDelete
  2. Thanks a ton !!! this really helped me a lot ....:)

    i need details knowledge of xpaths, where will i get it from ? please suggest

    ReplyDelete
  3. really super brother.

    ReplyDelete
  4. Thank you! This helped solving my problem!

    ReplyDelete
  5. Thanks a lot, nice tips but how can we use above Xpath expression in selenium Ide target field because I tried unfortunately didnt worked for me...could you please help me in that?

    ReplyDelete
  6. Click on first link
    selenium.click("xpath=(//a[@name='a1'])[position()=1]");
    selenium.waitForPageToLoad("80000");
    Click on second link
    selenium.click("xpath=(//a[@name='a1'])[position()=2]");
    selenium.waitForPageToLoad("80000");
    Click on last link
    selenium.click("xpath=(//a[@name='a1'])[last()]");
    selenium.waitForPageToLoad("80000");
    Click on thrid link
    selenium.click("xpath=(//a[@name='a1'])[position()=3]");
    selenium.waitForPageToLoad("80000");

    i try to use the above code with for loop :

    for (int i = 0; i < rowCount_tpsumry; i++)
    {
    selenium.click("xpath=(//a[@name='a1'])[position()=i]");
    }

    but it is not working.

    please suggest me to use it with for loop.

    Thanks in advance.

    ReplyDelete
    Replies
    1. After clicking on Link go back to the home page for each iteration.
      Use driver.navigate().to() for backward functionality.
      Code will work

      Delete
  7. for (int i = 0; i < rowCount_tpsumry; i++)
    {
    selenium.click("xpath=(//a[@name='a1'])[position()=i]");
    selenium.waitForPageToLoad("80000");
    }

    ReplyDelete
  8. nice.. one... very useful.. easy to understand..

    ReplyDelete
  9. nice.. one... very useful.. easy to understand..

    ReplyDelete
  10. Good one man. your explanation is easy to understand.

    ReplyDelete
  11. Great post! We are linking to this great post on our website.

    Keep up the great writing.

    Here is my web blog: elektronisk cigaret

    ReplyDelete
  12. Thanks , I've recently been searching for info about this subject for a while and yours is the best I've came
    upon so far. But, what about the bottom line? Are
    you sure in regards to the source?

    my web blog :: elektronisk cigaret

    ReplyDelete
  13. We stumbled over here coming from a different page and thought I may
    as well check things out. I like what I see so i am just following you.
    Look forward to finding out about your web page
    for a second time.

    my website ... e-cigaretter

    ReplyDelete
  14. You\'ll also business save them from having to pay First Quantum more than $1bn in compensation. I believe that the head is the" level playing field, with ministers who are looking for change. As business a communications professional, one of the biggest losers of today's sell-οff
    after the Bank of Japan govеrnоr, vοwed to achieѵe the deѕiгed bеnefits.


    mу wеb pаge ... tv.wheatworkspro.com

    ReplyDelete
  15. I needed to thank you for this fantastic read!! I absolutely enjoyed every little
    bit of it. I've got you book-marked to check out new stuff you post…

    Also visit my weblog: e-cigs

    ReplyDelete
  16. Hello there! This post couldn't be written much better! Reading through this article reminds me of my previous roommate! He constantly kept preaching about this. I will send this information to him. Pretty sure he will have a great read. Thanks for sharing!

    Review my weblog ryge væsker

    ReplyDelete
  17. I absolutely love your blog and find almost all of your post's to be exactly I'm looking for.
    Does one offer guest writers to write content available for you?
    I wouldn't mind creating a post or elaborating on a lot of the subjects you write in relation to here. Again, awesome weblog!

    Here is my blog: e-cigarette refill

    ReplyDelete
  18. I have read so many content on the topic of the blogger lovers except this article is actually
    a pleasant article, keep it up.

    Feel free to visit my blog ryge væsker

    ReplyDelete
  19. Pretty! This has been an incredibly wonderful post.
    Many thanks for supplying these details.

    Feel free to surf to my web blog: zemos98.org

    ReplyDelete
  20. Does your website have a contact page? I'm having problems locating it but, I'd like to shoot you an email.
    I've got some recommendations for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it grow over time.

    my weblog :: quit smoking fags

    ReplyDelete
  21. Hello, i think that i saw you visited my weblog so i came to “return the
    favor”.I am trying to find things to enhance my website!
    I suppose its ok to use some of your ideas!!

    Feel free to surf to my website ... billig ecigaret

    ReplyDelete
  22. Simply want to say your article is as astounding.
    The clarity in your post is simply spectacular and i could assume you are an expert on this subject.
    Well with your permission allow me to grab your RSS feed to keep up to date
    with forthcoming post. Thanks a million and
    please carry on the enjoyable work.

    Also visit my web-site ... tobacco e-liquid

    ReplyDelete
  23. Does your website have a contact page? I'm having problems locating it but, I'd like
    to shoot you an email. I've got some suggestions for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it develop over time.

    Also visit my website; awesomeindiefilms.doobious.org

    ReplyDelete
  24. I'm truly enjoying the design and layout of your website. It's a
    very easy on the eyes which makes it much more pleasant for me
    to come here and visit more often. Did you hire out a designer
    to create your theme? Excellent work!

    Also visit my webpage ... http://timemoney.com.ua/node/7268/

    ReplyDelete
  25. If some one wants expert view regarding blogging then i advise him/her to go to see this web site,
    Keep up the nice job.

    my web blog; billig e-væske

    ReplyDelete
  26. Great article with neatly explained. its pleasure to learn selenium from your article for beginners. thanks for sharing Best selenium training institute in chennai

    ReplyDelete
  27. From My search…Creating Experts provides Best SAP MM training with real time projects assistance. Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage. They also provide placement assistance in leading MNC companies across the globe according to the current requirements.
    And these are the Best SAP MM training institute which provides Real Time Hands on Training…
    Codedion Technologies-9003085882
    Creating Experts-8122241286
    They also providing both Classroom/Online Training

    ReplyDelete
  28. Selenium WebDriver fits in the same role as RC did, and has incorporated the original 1.x bindings. It refers to both the language bindings and the implementations of the individual browser controlling code. This is commonly referred to as just "WebDriver" or sometimes as Selenium 2.
    Selenium Training Institute in Chennai



    FREE SELENIUM TUTORIALS

    ReplyDelete
  29. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.


    Selenium Training in Bangalore

    ReplyDelete
  30. Thanks a lot very much for the high quality and results-oriented help.
    I won’t think twice to endorse your blog post to anybody who wants
    and needs support about this area.


    java training in chennai


    java training in bangalore

    ReplyDelete
  31. Thank you for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit.
    sap abap training online

    ReplyDelete
  32. 3D Animation Training in Noida

    Best institute for 3d Animation and Multimedia

    Best institute for 3d Animation Course training Classes in Noida- webtrackker Is providing the 3d Animation and Multimedia training in noida with 100% placement supports. for more call - 8802820025.

    3D Animation Training in Noida

    Company Address:

    Webtrackker Technology

    C- 67, Sector- 63, Noida

    Phone: 01204330760, 8802820025

    Email: info@webtrackker.com

    Website: http://webtrackker.com/Best-institute-3dAnimation-Multimedia-Course-training-Classes-in-Noida.php

    ReplyDelete
  33. Graphics designing training institute in Noida
    Best Graphics training institute in Noida, Graphic Designing Course, classes in Noida- webtrackker is providing the graphics training in Noida with 100% placement supports. If you are looking for the Best Graphics designing training institute in Noida For more call - 8802820025.

    Graphics designing training institute in Noida, Graphics designing training in Noida, Graphics designing course in Noida, Graphics designing training center in Noida

    Company address:
    Webtrackker Technology
    C- 67, Sector- 63, Noida
    Phone: 01204330760, 8802820025
    Email: info@webtrackker.com
    Website: http://webtrackker.com/Best-institute-for-Graphic-Designing-training-course-in-noida.php

    ReplyDelete
  34. Latest News in Hindi

    Latest News in Hindi- Hindustan channel is the best online web portal in india where you read the all latest indian news in hindi. if you are looking the Latest News in Hindi, live news channel, hindi news channel, live news channels in hindi, live hindi channels then hindustan channel is best for you.
    Latest News in Hindi

    Company address:
    C- 67, Sector- 63, Noida
    Phone: 01204330760, 8802820025


    URL: https://hindustanchannel.com

    ReplyDelete
  35. Sap fico training institute in Noida

    Sap fico training institute in Noida - Webtrackker Technology is IT Company which is providing the web designing, development, mobile application, and sap installation, digital marketing service in Noida, India and out of India. Webtrackker is also providing the sap fico training in Noida with working trainers.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.

    +91 - 8802820025
    0120-433-0760
    0120-4204716
    EMAIL: info@webtrackker.com
    Website: www.webtrackker.com

    ReplyDelete
  36. Sap fico training institute in Noida

    Sap fico training institute in Noida - Webtrackker Technology is IT Company which is providing the web designing, development, mobile application, and sap installation, digital marketing service in Noida, India and out of India. Webtrackker is also providing the sap fico training in Noida with working trainers.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.

    +91 - 8802820025
    0120-433-0760
    0120-4204716
    EMAIL: info@webtrackker.com
    Website: www.webtrackker.com

    ReplyDelete
  37. Sap fico training institute in Noida

    Sap fico training institute in Noida - Webtrackker Technology is IT Company which is providing the web designing, development, mobile application, and sap installation, digital marketing service in Noida, India and out of India. Webtrackker is also providing the sap fico training in Noida with working trainers.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.

    +91 - 8802820025
    0120-433-0760
    0120-4204716
    EMAIL: info@webtrackker.com
    Website: www.webtrackker.com

    ReplyDelete
  38. It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.
    python training in chennai
    Python Online training in usa
    python course institute in chennai

    ReplyDelete
  39. How to test a targeted checkbox only for say 4th checkbox always ,and the checkbox count may alter and position also changes.how to handle

    ReplyDelete
  40. SFDC Training path is supposed to assure which you study and enforce the thoughts of SFDC platform developer and bypass any required accreditation checks on your first attempt. Quality in elegance guidance will assist you spot how to make bigger salesforce highlights utilizing the state-of-the-art and automated abilities of SFDC code and visual pressure. The down to earth fingers-on mastering approach observed within the route will assure you land role prepared earlier than the cease of it. For best Salesforce Training in Noida aspirants needs to get training from authorised centre for ultimate solutions. Salesforce training in noida sector 63

    ReplyDelete
  41. Android training center in noida sector 15 Android is an open source and Linux-based Operating System Android is an open source and Linux-based Operating System for PDAs, for instance, mobile phones and tablet PCs. Android was made by the Open Handset Alliance, drove by Google, and distinctive associations. Android training in noida Webtrackker Technology offers a united approach to manage application headway for mobile phones which suggest engineers require create for Android, and their applications should have the ability to continue running on different contraptions controlled by Android.

    ReplyDelete
  42. Android training center in noida sector 3 The Android Open Source Project made a Compatibility Test Suite, or cts, that you can use to test an app's compatibility with the Apps working framework. Accessible free on the Web, the Webtrackker Technology best Android training institute and creates a report on each application's appropriateness for arrangement. What's more, Google arrangements to offer a Web-based service that will allow application developers to publish their CTS reports to a public database. And the last guarantees that a particular software application meets the standards required for sending on cell phones that run the Android working system.

    ReplyDelete
  43. Android, MAC & Phone gap is the most popular operating system using in tablet iPhone & smarts phone. Because of the rising the request and high offering of Apps based mobile devices, demanding of smart phone in the market is being going up step by step. The most reason of requesting this is Android is user friendly environment means that simple to use as contrast with other working frameworks in savvy phone.

    ReplyDelete
  44. Android, MAC & Phone gap is the most popular operating system using in tablet iPhone & smarts phone. Because of the rising the request and high offering of Apps based mobile devices, demanding of smart phone in the market is being going up step by step. The most reason of requesting this is Android is user friendly environment means that simple to use as contrast with other working frameworks in savvy phone.
    The applications programming designers need to much consider a long series of screen sizes, hardware specifications and configurations due to strong competition in the mobile software and changes within each of the platforms. The development of mobile applications is always developing, both as far as wage and jobs created.

    ReplyDelete
  45. Initially, Andy Rubin set up Android Incorporation in Palo Alto, California, United States in October, 2003. In seventeenth August 2005, Google secured android Incorporation. Beginning now and into the not too far-removed, it is in the support of Google Incorporation. The key administrators of Android Incorporation are Andy Rubin, Rich Miner, Chris White and Nick Sears. Originally proposed for camera yet moved to cutting edge PDAs later in this way of low market for camera in a way. Android is the moniker of Andy Rubin given by partners by respectability of his affection to robots. In 2007, Google reports the progress of OS. In 2008, HTC moved the key android adaptable.

    ReplyDelete
  46. I am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing. Great efforts.

    Looking for Big Data Hadoop Training Institute in Bangalore, India. Prwatech is the best one to offers computer training courses including IT software course in Bangalore, India.

    Also it provides placement assistance service in Bangalore for IT. Best Data Science Certification Course in Bangalore.

    Some training courses we offered are:

    Big Data Training In Bangalore
    big data training institute in btm
    hadoop training in btm layout
    Best Python Training in BTM Layout
    Data science training in btm

    ReplyDelete
  47. Effective blog with a lot of information. I just Shared you the link below for ACTE .They really provide good level of training and Placement,I just Had Automation Testing Classes in ACTE , Just Check This Link You can get it more information about the Automation Testing course.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  48. Hey guy's i have got something to share from my research work
    Tukui
    Skewed
    Mpi-Sws

    ReplyDelete
  49. Visit Bharat Go Digital Academy to learn the digital marketing skills in India.

    ReplyDelete
  50. Good blog,

    Digital Marketing, Digital Marketing Online Training, Digital Marketing Training Programs, Women Entrepreneurship, Women Entrepreneurship Training Programs, Digital marketing online video course, Women Entrepreneurship Online Certification Course, Business coaching, Training for Business owners, Business coaching for women, young entrepreneurs training

    https://www.eminentdigitalacademy.com

    ReplyDelete
  51. 1 Year Free Support and Access
    + 100% Practical Job Oriented Training.
    + 12 Types of Certifications to boost your resume
    + 2 Month Internship program on Live Projects.
    + Trending & most important skill required these days.

    ReplyDelete

  52. That is nice article from you , this is informative stuff . Hope more articles from you . I also want to share some information about Pet Dermatology in Vizag

    ReplyDelete