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

Locators in Selenium

Uploaded by

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

Locators in Selenium

Uploaded by

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

Locators in

Selenium
BY SAURABH DHINGRA
Locators in Selenium
1. Id
2. Name
3. Classname
4. Tagname
5. Linktext
6. Partial Linktext
7. Xpath
8. Css Selectors
XPath Definition
XPath is the path written using html
tags(or XML tags) and their attributes to
reach to a particular node (or web
element) in an HTML page or XML page.

Types of XPath:
Absolute XPath
Relative XPath
Absolute XPath
Absolute XPath starts from the beginning
of the page. As in an html page, the first
tag is HTML. So all absolute XPath always
start with html tag and then accessing
immediate child to reach to a node.
“/” is used to access an immediate child of
the parent tag.
Example:
html/body/table/tbody/tr[2]/td/input
Relative XPath
Relative XPath starts from anywhere on the
page.
“//” is used to access any child of the
parent tag.
Basic
Syntax: //htmlTagname[@attribute=’value’]
Example: //input[@type=’text’] –
It represents xpath of a WebElement which
is represented by an input tag and also has
an attribute type = ‘text’.
Absolute Vs Relative
Absolute xpath are fast, but they have one
disadvantage that if there is an addition or
deletion of some nodes in between, then
they fail to work.
In such a situation relative xpath are much
better, and one should be good at writing
dynamic xpath. There are some methods,
operators, and axes which are available in
xpath which can help to locate elements
uniquely.
How to Identify
Dynamic Web
Elements?
Dynamic Web Elements are one which
changes dynamically like – their attributes
such as Id or Classname are changing, or
text associated is changing.
Best possible way to identify these elements
is to first search for another web element
which stable and also can be identified
uniquely, then on this element using
methods, operators, or axes, one can reach
to the desired element.
Methods in XPaths:
Contains():
Contains is a method used when the value of
any attribute changes dynamically. It can search
an element with partial information.
Starts-with()–
This method can be used when we are
searching for web elements matching the start
of the text of the attributes passed. The text()
method can also be used which will match the
starting of the text.
Text() –
This method is used when we are searching for
elements matching the exact text.
Operators in XPath:
AND and OR are two operators available in
xpath.

AND operator, when applied with multiple


attributes and/or methods, identifies a web
element only when all the attributes are
pointing to that element.

OR operator, when applied to multiple


attributes, identifies a web element only when
any one of the attributes points to that
element.
AXES in Selenium WebDriver:
Axis in xpath points the xpath processor to
the direction in which it should navigate in
the hierarchy of the html nodes.
Basic Syntax:
//html_tag[@attribute=’value’]//axes::html_t
ag
Frequently used
Axes in XPath:
1. Ancestor – contains ancestor of the context
node.
2. Child – includes the child of the context
node.
3. Following – select the elements which follow
after the context node.
4. Preceding – choose the elements which
precede before the context node.
5. Following-Sibling – choose the sibling
coming after the context node.
6. Preceding-Sibling – choose the sibling
coming before the context node.
7. Parent – contains the parent of the context
node.
CSS Selectors
Agenda –
What is CSS Selector locator?
Basic Syntax?
Operators in CSS Selectors?
CSS Selector
CSS is "Cascading Style Sheets" and it is
defined to display HTML in structured and
colorful styles are applied to webpage.
Selectors are patterns that match against
elements in a tree, and as such form one of
several technologies that can be used to
select nodes in an HTML/XML document.
CSS is much faster and simpler than the
XPath.
Basic Syntax –
htmltag[attribute='value’]
Operators in CSS Selector
Using class in css
.(dot) operator is used in classes
.small.cbx.btn.btn-s.btn-
ter.tab.tgl_button.center_b
Using Id:
# (hash) operator is used
#ListViewInner -- example using Id
Operators in CSS Selector
^ (power symbol) -- its like starts with
-- starting of the string
input[id^=ema]
$ (dolar symbol) -- its like ends with
-- matches ending text
* (astrick) -- its like contains method
-- matches some part of the string

You might also like