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

02_02_Selenium WebDriver Locators_Part1

Uploaded by

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

02_02_Selenium WebDriver Locators_Part1

Uploaded by

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

RBVH/ENG1

Selenium WebDriver
Locators
May-2019

Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
2
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
RBVH/ENG1

What are Locators?


 Locator is a command that tells Selenium IDE which GUI elements (
say Text Box, Buttons, Check Boxes etc) its needs to operate on.
Identification of correct GUI elements is a prerequisite to creating
an automation script. But accurate identification of GUI elements is
more difficult than it sounds. Sometimes, you end up working with
incorrect GUI elements or no elements at all! Hence, Selenium
provides a number of Locators to precisely locate a GUI element

Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
 This is the most common way of locating elements since ID's are
supposed to be unique for each element.
 Target Format: id=id of the element

Locating by ID
 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

Locating by Name
Locating by Link Text

 This type of locator applies only to hyperlink texts. We access the link
by prefixing our target with "link=" and then followed by the
hyperlink text.
 Target Format: link=link_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.
Find Element and FindElements in Selenium
WebDriver

 Why do you need Find Element/s command?


 Interaction with a web page requires a user to locate the web
element. Find Element command is used to uniquely identify a
(one) web element within the web page. Whereas, Find Elements
command is used to uniquely identify the list of web elements
within the web page. There are multiple ways to uniquely identify
a web element within the web page such as ID, Name, Class
Name, Link Text, Partial Link Text, Tag Name and XPATH.
WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));

FindElement command syntax:

 Find Element command takes in the By object as the parameter and


returns an object of type WebElement. By object in turn can be used
with various locator strategies such as ID, Name, Class Name, XPATH
etc. Below is the syntax of FindElement command in Selenium web
driver.

WebElement elementName =
driver.findElement(By.LocatorStrategy("LocatorValue"));
Locator Strategy can by any of the
following values.
 ID
 Name
 Class Name
 Tag Name
 Link Text
 Partial Link Text
 XPATH
FindElement

 Locator Value is the unique value using which a web element can be
identified. It is the responsibility of developers and testers to make
sure that web elements are uniquely identifiable using certain
properties such as ID or name.
 Example:

WebElement loginLink =
driver.findElement(By.linkText("Login"));
FindElements command syntax:

 Find Elements command takes in By object as the parameter and


returns a list of web elements. It returns an empty list if there are no
elements found using the given locator strategy and locator value.
Below is the syntax of find elements command.
List<WebElement> elementName =
driver.findElements(By.LocatorStrategy("LocatorVa
lue"));
Example:
List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));
Find element Vs Find elements
Summary
Find Element command returns the web element that
matches the first most element within the web page.

Find Elements command returns a list of web


elements that match the criteria.
Find Element command throws NoSuchElement
exception if it does not find the element matching
the criteria.
Find Elements command returns an empty list if
there are no elements matching the criteria
RBVH/ENG1

Reference
 https://www.guru99.com/selenium-tutorial.html

15

Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.

You might also like