Day 1 - Selenium IDE - Session 2 - Locators
Day 1 - Selenium IDE - Session 2 - Locators
by
Venkataraman Chandrasekar
Locators
2
ID
It is a unique reference for a web object that the developer sets while writing the code.
The ID is no doubt the easiest way to locate an element on a web page.
Example
3
NAME
Every form has input fields with unique names and we can use these names to locate
elements.
Name of an element is provided in the HTML tag for the field.
Example
<input id=”techcanvass” name=”admin” class=”required” type=”text”/>
4
LINK TEXT
It is a good way to find/locate the links on a page. It is used to select the link element
which contains the matching text.
Example
<a href=”http://www.techcanvass.com“>Click Here</a>
5
PARTIAL LINK TEXT
Example
<a href=”http://www.techcanvass.com“>Click Here</a>
6
TAG NAME
We can also locate elements by using Tag names as defined in DOM (Document object
Model).
Locating Element By Tag Name is not too much popular because in most of cases, we
have better alternatives of element locators.
Example
//GET ALL LINKS IN A WEBPAGE
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
7
CSS CLASS
CSS Class locator uses the CSS class of an element on the web page to locate.
Example
WebElement element =driver.findElement(By.className(“techcanvass ”));
8
CSS SELECTOR
Example
WebElement element = driver.findElement(By.cssSelector(“input#txtName”));
9
XPATH
FirePath is a simple tool to check the XPATH of any element. Firepath can be installed as an
extension or add-on to the browsers.
10
ABSOLUTE PATH
It starts from the root element within the web page or part of the page and goes to identify
the target element
EXAMPLE
/HTML/head/body/table/tr/td
To use locators like the XPath is easy as you give the direct element path. But the XPath
would break when the element structure changes.
11
RELATIVE PATH
EXAMPLE
//table/tr/td
12
DEMO
13
SUMMARY
o XPATH types
Quiz
19
THANK YOU
Manipal ProLearn
#7, Service Road, Pragathi Nagar, Electronic City,
Bengaluru 560100
[email protected] | manipalprolearn.com