Saturday, September 22, 2012

Dealing with web elements by different locators

Let’s consider the following sample HTML code snippet as a reference point.
  1. <!DOCTYPE HTML>
  2. <html>
  3.     <head>
  4.         <script type="text/javascript">
  5.             function jsFunction(){
  6.                 alert("Welcome to Selenium UI Automation Testing !");
  7.             }
  8.         </script>
  9.     </head>
  10.     <body>
  11.         <input type="submit" id="webButton" onclick="jsFunction()" value="Click It" />
  12.     <a href="http://aksahu.blogspot.in/">Go Home</a>
  13.     </body>
  14. </html>
Lets start by taking example of any of the selenium command say "click"
You can use same fashion for any of the selenium command like i am using for click.
The click command emulates a click operation for a link, button, checkbox or radio button. It takes a locator (an identifier for which HTML element the command refers to) as an argument.

Using Selectors with Click Command:

xpath

This allows clicking on an element using an XPath expression. Example

selenium.click("xpath=//input[@name=webButton' and @type='submit']");

css

CSS locator is used with Selenium commands to uniquely identify an object or element on a web page. Example,

selenium.click("css=input[name=webButton]”);

name

The name selector is used to click the first element with the specified @name attribute. Example,

selenium.click("name=webButton”);
or
selenium.click("webButton");

id

This allows click on an element with the specified @id attribute. Example,

selenium.click("id=webButton”);

link

This allows clicking on a link element which contains text matching the specified pattern. Example,

selenium.click("link=Go Home”);


1 comment: