Selenium Locators
Selenium Locators
com
+918130877931
kasper-analytics
kasperanalytics.com
+918130877931
By ID
Locate elements using the 'id' attribute of the HTML element. This is usually unique
and preferred.
By Name
Locate elements using the 'name' attribute. Not always unique, but useful for
forms.
By Link-Text
Locate anchor tags (`<a>`) using the exact link text. Use when you know the full link
text.
Locate anchor tags using a partial match of the link text. Good for dynamic text.
By Tag Name
By Class Name
Locate elements using the class attribute. Use when elements share common
styles.
By CSS Selector
Locate elements using CSS selectors (e.g., `.class Name`, `#id`). Preferred for
performance.
kasper-analytics
kasperanalytics.com
+918130877931
Locators in Selenium
kasper-analytics
kasperanalytics.com
+918130877931
Example
1. Using id –
All objects on a page are not having id attribute, it’s not realistic. In some cases
developers make it havingnon-unique ids on a page or auto-generate the ids, in
both cases it should be avoided.
2. Using Name –
By using name attribute we can find element on DOM, name attributes are not
unique in a page all time. With the Name attribute value matching the location
will be returned. If no element has a matching nameattribute, a
“NoSuchElementException” will be raised.
kasper-analytics
kasperanalytics.com
+918130877931
3. Using Link –
With this you can find elements of “a” tags(Link) with the link names. Use this
when you know link text used within an anchor tag.
4. Using xPath –
While DOM is the recognized standard for navigation through an HTML element
tree, XPath is the standard navigation tool for XML and an HTML document is also
an XML document (xHTML). XPath is used everywhere where there is XML. Xpath
has a fixed structure (syntax). See below –
kasper-analytics
kasperanalytics.com
+918130877931
Xpath = //*[@id=’username’]
Xpath = //input[@id=’username’]
Xpath = //form[@name=’loginForm’]/input[1]
Xpath = //*[@name=’loginForm’]/input[1]
Dynamic elements are those elements who changes is attribute on every runtime.
Xpath Axes are used tofind the xpath of the such dynamic elements.
kasper-analytics
kasperanalytics.com
+918130877931
Xpath Axes –
XPath Axes are the methods used to find dynamic elements. XPath axes search
different nodes in XML document from current context node. XPath expression
select nodes or list of nodes on the basis of attributes likeID , Name, Classname,
etc. from the XML document .
Following:
Selects all elements in the document of the current node( ) in following image,
input box is thecurrent node.
There are 3 "input" nodes matching by using "following" axis- password, login and
reset button.
If you want to focus on any particular element then you can use the below XPath
method:
kasper-analytics
kasperanalytics.com
+918130877931
Ancestor:
The ancestor axis selects all ancestors element (parent, grandparent,…etc.) of the
current node as shown in the below screen. In the below expression, we are
finding ancestors element of the current node("ENTERPRISE TESTING" node).
kasper-analytics
kasperanalytics.com
+918130877931
Child:
Selects all children elements of the current node (Java) as shown in the below
screen.
There are 71 "li" nodes matching by using "child" axis. If you want to focus on any
particular element then You can change the xpath according to the requirement
by putting [1],[2] and so on.
Preceding
Select all nodes that come before the current node as shown in the below screen.
In the below expression,
itidentifies all the input elements come before "LOGIN" button that is Userid and
password input element.
kasper-analytics
kasperanalytics.com
+918130877931
There are 2 "input" nodes matching by using "preceding" axis. If you want to focus
on any particularelement then You can change the xpath according to the
requirement by putting [1],[2] and so on.
Following sibling
Select the following siblings of the context node. Siblings are at the same level of
the current node as shownin the below screen. It will find the element after the
current Login node . One input nodes matching by using "following-sibling" axis
kasper-analytics
kasperanalytics.com
+918130877931
Parent: Selects the parent of the current node as shown in the below screen.
There are 65 "div" nodes matching by using "parent" axis. If you want to focus on
any particular element thenYou can change the XPath according to the
requirement by putting [1],[2] and so on.
kasper-analytics
kasperanalytics.com
+918130877931
Self:
Selects the current node or 'self' means it indicates the node itself as shown in the
below screen. One node matching by using "self " axis. It always finds only one
node as it represents self-element.
Descendant:
Selects the descendants of the current node as shown in the below screen. In the
below expression, it identifies all the element descendants to current element (
'Main body surround' frame element) which means down under the node (child
node , grandchild node, etc.).
kasper-analytics
kasperanalytics.com
+918130877931
There are 12 "link" nodes matching by using "descendant" axis. If you want to focus
on any particular elementthen You can change the XPath according to the
requirement by putting [1],[2]. and so on.
There is a debate on the performance of CSS Locator and XPath locator. Most of
the automation testers believe that using CSS selectors makes the execution of
script faster compared to XPath locator. CSS Selector locator isalways the best
way to locate elements on the page. CSS is always same irrespective of browsers.
If fixed part is at starting use (^) e.g. input [id^=’XXXXXX’] If fixed part is at mid
use (*) e.g. input [id*=’XXXXXX’] If fixed part is at end use ($) e.g. input
[id$=’XXXXXX’]
kasper-analytics
kasperanalytics.com
+918130877931
Tag and ID :
If multiple elements have the same HTML tag and class, then the first one will be
recognized.
kasper-analytics
kasperanalytics.com
+918130877931
kasper-analytics
kasperanalytics.com
+918130877931
SUB-STRING MATCHES:
Suppose
<input="Employee_ID_001">
Starts with (^): To select the element, we would use ^ which means ‘starts with’
css=input[id^='Em']
kasper-analytics
kasperanalytics.com
+918130877931
Ends with ($): To select the element, we would use $ which means ‘ends with’.
css=input[id^=’001’]
Contains (*): To select the element, we would use * which means ‘sub-string’
css=input[id*='id']
kasper-analytics
kasperanalytics.com
+918130877931
<li>Selenium</li>
<li>QTP</li>
<li>Sikuli</li>
</ul>
css="ul#automation li:nth-of-type(2)"
Similarly, To select the last child element, i.e. ‘Sikuli’, we can use
css="ul#automation li:last-child"
END
kasper-analytics
Need any experience/support on
Hands-on Live Project
and Live Frameworks.
Please
Connect