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.


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