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 check boxes 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 check boxes are there having same type and same name.
It is really tough to select third or fifth.

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

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

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

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");

How to Write in iFrame in selenium

When you write any mail in gmail you can see the body is a iframe we can write in it using

String  = selenium.getEval("var bodytext=\" Writing text in iframe body with the help of http://automationtricks.blogspot.com \";  var iframe_locator=\"css=table:contains('Subject:') +*  iframe\";  
 var iframe_body=selenium.browserbot.findElement(iframe_locator).contentWindow.document.body;   if (browserVersion.isChrome){  
  iframe_body.textContent=bodytext;
 }  else if(browserVersion.isIE){
 iframe_body.innerText=bodytext;
 }
");


7 comments:

  1. If all check boxes size increasing or decreasing dynamically and all check boxes having same values then how to check each and every check boxes.

    ReplyDelete
  2. This is very informative blog and nice article , I really like your technique of writing a blog. More screenshots gives the clear representation.. I book marked it to my bookmark site list and will be checking back in the near future Selenium Training in Chennai gives you the entire stuff about all the Selenium Components. Among that, the above mentioned IDE is the baby steps to learn other 2 components.

    ReplyDelete
  3. Hello Ramesh,


    A nice article here with some useful tips for beginners. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them. Here are some more link which I also useful for freshers.
    Free Selenium Tutorials |

    ReplyDelete
  4. I have read your blog and i got a very useful and knowledgeable information from your blog.You have done a great job . If anyone want Java Training in Chennai, Please visit our page Java Training in Chennai

    ReplyDelete
  5. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Selenium Training in Chennai

    ReplyDelete
  6. Great read, Selenium has brought evolution to automation world.Thanks for taking a time to share a wonderful article.I share this website my office mate and friend circle.I'll be to provide that real-time Selenium Automation Training in Chennai.We having to provide that Latest version selenium 3.0.
    Selenium Training in Chennai

    ReplyDelete