Locators in Selenium IDE
Locators in Selenium IDE
Locators in Selenium IDE
ID
Name
Link Text
CSS Selector
Tag and ID
There are commands that do not need a locator (such as the "open"
command). However, most of them do need Locators.
The choice of locator depends largely on your Application Under
Test. In this tutorial we will toggle between facebook ,
newtours.demoaut on basis of locators that these applications support.
Likewise in you testing project you will select any of the above listed
locators based on your application support.
Locating by ID
This is the most common way of locating elements since ID's are
supposed to be unique for each element.
Step 2. Launch Selenium IDE and enter "id=email" in the Target box.
Click the Find button and notice that the "Email or Phone" text box
becomes highlighted with yellow and bordered with green, meaning,
Selenium IDE was able to locate that element correctly.
Locating by Name
Locating elements by name are very similar to locating by ID, except
that we use the "name=" prefix instead.
Target Format: name=name of the element
In the following demonstration, we will now use Mercury Tours because
all significant elements have names.
Step 1. Navigate to http://newtours.demoaut.com/ and use Firebug to
inspect the "User Name" text box. Take note of its name attribute.
Step 2. Using Firebug, notice that the Round Trip and One Way radio
buttons have the same name "tripType." However, they have different
VALUE attributes so we can use each of them as our filter.
Step 3.
We are going to access the One Way radio button first. Click the
first line on the Editor.
Step 4. Click the Find button and notice that Selenium IDE is able to
highlight the One Way radio button with green - meaning that we are
able to access the element successfully using its VALUE attribute.
Step 5. Press the "X" key in your keyboard to execute this click
command. Notice that the One Way radio button became selected.
You can do the exact same thing with the Round Trip radio button, this
time, using "name=tripType value=roundtrip" as your target.
First, make sure that you are logged off from Mercury Tours.
Using Firebug, inspect the "REGISTER" link. The link text is found
between and tags.
In this case, our link text is "REGISTER". Copy the link text.
Step 3. Copy the link text in Firebug and paste it onto Selenium IDE's
Target box. Prefix it with "link=".
Step 4. Click on the Find button and notice that Selenium IDE was
able to highlight the REGISTER link correctly.
Tag and ID
Tag and class
Tag and attribute
Tag, class, and attribute
Inner text
When using this strategy, we always prefix the Target box with "css="
as will be shown on the following examples.
Description
css=tag#i
d
Description
css=tag.clas
s
Take note that when multiple elements have the same HTML
tag and name, only the first element in source code will be
recognized. Using Firebug, inspect the Password text box in Facebook
and notice that it has the same name as the Email or Phone text box.
The reason why only the Email or Phone text box was highlighted in
the previous illustration is that it comes first in Facebook's page
source.
Syntax
css=tag[attribute=value
]
Description
accessed
[ and ] = square brackets within which a
When multiple elements have the same HTML tag and attribute,
only the first one will be recognized. This behavior is similar to
locating elements using CSS selectors with the same tag and class.
Description
css=tag.class[attribute=value]
being accessed
. = the dot sign. This should always be
accessed
[ and ] = square brackets within which
chosen attribute.
Step 2. We will access the 'Email or Phone' text box first, thus, we
will use a tabindex value of 1. Enter "css=input.inputtext[tabindex=1]"
in Selenium IDE's Target box and click Find. The 'Email or Phone' input
box should be highlighted.
Step 3. To access the Password input box, simply replace the value of
the tabindex attribute. Enter "css=input.inputtext[tabindex=2]" in the
Target box and click on the Find button. Selenium IDE must be able to
identify the Password text box successfully.
Description
Step 3. This time, replace the inner text with "Boston" so that your
Target will now become "css=font:contains("Boston")". Click Find. You
should notice that the "Boston to San Francisco" label becomes
highlighted. This shows you that Selenium IDE can access a long label
even if you just indicated the first word of its inner text.
Locating by XPath
XPath is the language used when locating XML (Extensible Markup
Language) nodes. Since HTML can be thought of as an implementation
of XML, we can also use XPath in locating HTML elements.
Advantage: It can access almost any element, even those
without class, name, or id attributes.
Disadvantage: It is the most complicated method of
identifying elements because of too many different rules and
considerations.
Fortunately, Firebug can automatically generate XPath locators. In the
following example, we will access an image that cannot possibly be
accessed through the methods we discussed earlier.
Step 1. Navigate to Mercury Tours Homepage and use Firebug to
inspect the orange rectangle to the right of the yellow "Links" box.
Refer to the image below.
Step 2. Right click on the element's HTML code and then select the
"Copy XPath" option.
Step 3. In Selenium IDE, type one forward slash "/" in the Target box
then paste the XPath that we copied in the previous step. The entry
in your Target box should now begin with two forward slashes
"//".