0% found this document useful (0 votes)
69 views

Day 1 - Selenium IDE - Session 2 - Locators

This document discusses locators in Selenium. It explains that locators provide a mechanism to access HTML elements on a web page so that test cases can be written to execute on those elements. Selenium uses 8 locator types, including ID, name, link text, partial link text, tag name, CSS class, CSS selector, and XPATH. It then provides examples and explanations of each locator type, as well as discussing absolute and relative XPATHs. The document concludes with a demo of using different locators and finding XPATHs in the browser.

Uploaded by

begum
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Day 1 - Selenium IDE - Session 2 - Locators

This document discusses locators in Selenium. It explains that locators provide a mechanism to access HTML elements on a web page so that test cases can be written to execute on those elements. Selenium uses 8 locator types, including ID, name, link text, partial link text, tag name, CSS class, CSS selector, and XPATH. It then provides examples and explanations of each locator type, as well as discussing absolute and relative XPATHs. The document concludes with a demo of using different locators and finding XPATHs in the browser.

Uploaded by

begum
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Day 1 – Session 2 - Locators

by
Venkataraman Chandrasekar
Locators

 Locators provide a mechanism to access the HTML elements on a web page.


 Once we locate an element on any web page, we can write test cases to execute on them.
 Selenium uses 8 locator types. They are:
 ID
 Name
 LinkText
 Partial Link Text
 Tag Name
 CSS Class
 CSS Selector
 XPATH

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

<input id=”techcanvass” class=”required” type=”text”/>

WebElement element = driver.findElement(By.id(“techcanvass”));

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”/>

WebElement locator = driver.findElement(By.name(“admin”));

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>

WebElement link = driver.findElement(By.linkText(“Click Here”));

5
PARTIAL LINK TEXT

 There is a difference between link text and partial link text.


 For locating element by link text, we need to use full word ‘Click Here‘.
 But in case of locating element by partial link text, we can locate element by partial
word ‘Click’ too.

Example
<a href=”http://www.techcanvass.com“>Click Here</a>

WebElement plink = driver.findElement(By.PartialLinkText(“Click”));

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

 CSS Selector is one of the most widely used locators.


 It uses the syntax of CSS style sheets as used in HTML

Example
WebElement element = driver.findElement(By.cssSelector(“input#txtName”));

9
XPATH

 XPath is another technique for locating an element.


 XPath is a W3C recommendation and uses XML document syntax for locating elements.
 It defines the path to any element using XML path language

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

 starts from the middle of the document.


 It starts with // instead of /.
 The relative XPath are easy to manage as they are short and concise.
 It is also better than the previous XPath style as it may survive the changes in the Page
HTML to a certain degree.
 Though, building a relative XPath is time-consuming and quite difficult as you need to
check all the nodes to form the path.

EXAMPLE
 //table/tr/td

12
DEMO

 Identify the element by different locators


 Installation of firebug and firepath
 Finding XPATH using Chrome browser

13
SUMMARY

Key points discussed in this webinar:

o Different Locators in Selenium

o XPATH types
Quiz

1. Among the following, which one is an an example of Absolute


XPATH?
A. /HTML/head/body/table/tr/td
B. //table/tr/td
Quiz

1. Among the following, which one is an an example of Absolute


XPATH?
A. /HTML/head/body/table/tr/td
B. //table/tr/td
Quiz

2. Among the following, which one is an valid locator?


A. id
B. name
C. property
D. css
Quiz

2. Among the following, which one is an valid locator?


A. id
B. name
C. property
D. css
Lab

 Refer the Day 1 Exercises


Q6 to Q8
 Duration : 30 Minutes

19
THANK YOU

Manipal ProLearn
#7, Service Road, Pragathi Nagar, Electronic City,
Bengaluru 560100
[email protected] | manipalprolearn.com

You might also like