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.


54 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. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium

    ReplyDelete
  27. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.
    Selenium

    ReplyDelete
  28. It was very nice blog to learn about SAP HR.Thanks for sharing new things.SAP HR

    ReplyDelete
  29. It was very nice blog to learn about Selenium.Thanks for sharing new things.selenium training in chennai

    ReplyDelete
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. Ciitnoida provides Core and java training institute in

    noida
    . We have a team of experienced Java professionals who help our students learn Java with the help of Live Base Projects. The object-

    oriented, java training in noida , class-based build

    of Java has made it one of most popular programming languages and the demand of professionals with certification in Advance Java training is at an

    all-time high not just in India but foreign countries too.

    By helping our students understand the fundamentals and Advance concepts of Java, we prepare them for a successful programming career. With over 13

    years of sound experience, we have successfully trained hundreds of students in Noida and have been able to turn ourselves into an institute for best

    Java training in Noida.

    java training institute in noida
    java training in noida

    ReplyDelete
  36. Best Summer Internship In Noida

    These technologies prepare individuals for fields like software programming, technical support, graphic design, software testing, business analytics. Embedded Systems, Industrial Automation Training and more. Candidates go through a series of comprehensive practical sessions where they work on live problems and implement solutions on real-time basis.
    It has a dedicated placement cell which provides 100% placement assistance to students. The benefits that a student gets out of summer training are innumerable. CIITN,Best Summer training Center for B.Tech/CS/CSE/IT/ BCA/MCA/ B.E /M.tech / B.sc/ M.sc/ Engineering Student has already accomplished itself successfully in the field of Training and Development after setting milestones and bringing smiles to the faces of more than 1 Lakh students. CIITN was incorporated in the year 2002 and over the years CIITN has grown remarkably into the greatest training giant of Northern India.
    There is no looking back to technology! Those passionate for it need not worry about the professional prospects it offers. Courses and trainings in the field of Computer Science and Applications open a wide array of choices for individuals. All you need to do it prepare yourself right!

    Summer Internship Course In Noida

    Summer Internship Training In Noida

    ReplyDelete
  37. Yiioverflow presenting one of the best and high performance PHP framework. Fast, secure and extremely professionals are developing applications. We guide to implement mobile app development and SOA hybrid applications.Code in Nodejs, Angular,Ionic,ReactJS and Yiiframework.

    ReplyDelete
  38. 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
  39. Artificial intelligence Training in noida
    Artificial intelligence Training in noida-Artificial Intelligence Training in Noida, Artificial Intelligence Training classes in Noida, Artificial Intelligence Training classes in Noida, Artificial Intelligence Training

    by Real time ARTIFICIAL INTELLIGENCE Experts, Big-Data and ARTIFICIAL INTELLIGENCE Certification Training in Noida



    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



    Our Other Courses:


    artificial intelligence Training in noida

    SAS Training Institute in Delhi

    SAS Training in Delhi

    SAS Training center in Delhi

    Sap Training Institute in delhi

    Sap Training in delhi

    Best Sap Training center in delhi

    Best Software Testing Training Institute in delhi

    Software Testing Training in delhi

    Software Testing Training center in delhi

    Best Salesforce Training Institute in delhi

    Salesforce Training in delhi

    Salesforce Training center in delhi

    Best Python Training Institute in delhi



    Python Training in delhi


    Best Android Training Institute In delhi


    Best Python Training center in delhi


    Android Training In delhi


    best Android Training center In delhi

    ReplyDelete

  40. Best Solidworks training institute in noida

    SolidWorks is a solid modeling computer-aided design (CAD) and computer-aided engineering (CAE) computer program that runs on Microsoft Windows. SolidWorks is published by Dassault Systems. Solid Works: well, it is purely a product to design machines. But, of course, there are other applications, like aerospace, automobile, consumer products, etc. Much user friendly than the former one, in terms of modeling, editing designs, creating mechanisms, etc.
    Solid Works is a Middle level, Main stream software with focus on Product development & this software is aimed at Small scale & Middle level Companies whose interest is to have a reasonably priced CAD system which can support their product development needs and at the same time helps them get their product market faster.

    Company Address:
    WEBTRACKKER TECHNOLOGY (P) LTD.
    C-67,Sector-63,Noida,India.
    E-mail: info@webtracker.com
    Phone No: 0120-4330760 ,+91-880-282-0025

    webtrackker.com/solidworks-training-Course-institute-in-noida-delhi

    ReplyDelete
  41. 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
  42. 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
  43. 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