Selenium - Locators & Element location Strategies
Locators & Element location Strategies
Automating any application requires identification of the element & interact with it. Identification of Element is achieved using locators.
contain one or more elements/tags/nodes
An element may contain content/data or may contain other elements as child nodes.
Each element should have starting tag and ending tag. The element content will reside in between these two tags.
Apart from the content each tag may have attributes. These attributes will give extra information about the element.
Locator are used for identifying the elements on the web page uniquely
a.
ID
b.
Name
c.
Class name - with spaces – not allowed –
[Compound class names not allowed error] – when required to identify group of
elements like radio buttons, checkboxes
2.
Location strategies for collection of elements
a.
Tagname - to identify group of elements
3.
Link text – when Tagname is anchor tag <a>
4.
Partial link text
5.
CSS selector
6.
Xpath
a.
Based on attribute -
//tagName[@attribute=’value’]
b.
Based on text - //tagname[text()=’value’]
c.
Based for collections – (Xpath)[index]
Advanced Xpath - Axes
1.
Child to Parent/ancestor & vice versa
a.
Child to Parent – child xpath/parent::parent
tagname
b.
Child to Grand Parent – child
xpath/ancestor::ancestor tagname
c.
Parent to child – Parent xpath/child tagname
d.
Parent to Grand Children – (Parent xpath//child
tagname)[index]
2.
Siblings – belonging under same parent tag
a.
Xpath/following-sibling::tagname
b.
Xpath/preceding-sibling::tagname
3.
Siblings[cousins] – belonging under different
parent sibling tags
a.
Xpath/following::tagname
b.
Xpath/preceding::tagname
XPath
functions
- Count()
- Contains() - //tagname[contains(text(),
‘’)]
- Starts-with() -
//tagname[starts-with(@id, ‘’)]
- ends-with() - //tagname[ends-with(@id, ‘’)]
- Normalize-space() -
//tagname[text()[normalize-space() = 'some
label']
CSS Selector:
1. TagName
usage of the tag name individually to locate the element is
not a good practise as multiple Elements may have same TagName.
Form
Input
2. Id
In order to use ID as locator we need to use ‘#’ symbol in
front of ID.
#IDValue
3. ClassName
In order to use Class Name as locator we need to use dot
symbol in front of class name.
.ClassName
4. Attributes
Single Attribute
TagName[attribute = ‘attributeValue’]
Multiple Attributes
tagName[attribute1=’attrValue1’][attribute2=’attrValue2’]
5. Contains
Needed to use ‘*’ symbol to identify the element.
tagName[attribute*=’partialAttributeValue’]
6. Starts-with
Needed to use ‘^’ symbol to identify the element.
tagName[attribute^=’AttributeStartValue’]
7. Ends With
Needed to use ‘$' symbol to identify the element.
tagName[attribute$=’attributeEndingValue’]
How to verify the created XPath/CSS is correct?
1.Press F12 to open up Chrome DevTools.
2.Elements panel should be opened by default.
3.Press Ctrl + F to enable DOM searching in the panel.
4.Type in XPath or CSS selectors to evaluate.
If there are matched elements, they will be highlighted in
DOM.
Xpath - $x(“”)
CSS Selector - $(“”)
Comments
Post a Comment