Locators in Selenium WebDriver
Introduction
UI automation tools like Selenium WebDriver mostly rely on locators to identify element and then perform particular action on identified web elements. Selenium supports multiple identification methods to identify web elements. Today we discuss pros and cons of selecting particular element identification method over other.
Selenium Locators:
Selenium locators are methods used to identify particular web element in HTML page. Below eight locators that selenium provides are listed below:
- ClassName - Uses class attribute to identify element
- Easy to identify, locates the element based on value of class attribute
- If the value of class attribute is not unique, first occurrence in the DOM is located
- CssSelector - Css in web page is used to identify element
- Challenging to identify, locates the element based on pattern in combination of different attributes in DOM and element's Id, class, tag etc...
- It can identify everything XPath can and is typically faster than XPath.
- Each browser has different XPath engine but Css identification is same irrespective of browser.
- CssSelector can traverse from parent to child only as opposed to child to parent in XPath.
- XPath - XPath is used to identify element
- Challenging to identify, locates the element based on pattern in combination of different attributes in DOM and element's Id, class, tag etc...
- It can identify everything CssSelector can and is typically slightly slower than CssSelector.
- XPath can traverse from parent to child and up the DOM from child to parent.
- XPath handles text recognition better.
- Name - Name attribute is used to identify element
- Easy to identify, locates element based on value of name attribute
- Name may or may not be unique
- Id - Id attribute is used to identify element
- Easy to identify, locates element based on value of Id
- Id attribute has to be unique in DOM
- LinkText - Text in hyperlinks is used to identify element
- Easy to identify, locates element based on value of text in hyperlink
- Cannot identify element having text which are not in hyperlink
- PartialLinkText - Part of Text in hyperlinks is used to identify element
- Easy to identify, locates element based on part of text value in hyperlink
- Cannot identify element having text which are not in hyperlink
- TagName - Uses Tag to identify element
- Easy to identify, locates element based on tag name in DOM
- Tag names may not be unique
Approach for selecting particular locator:
In a typical framework one should provide option to support all selenium locators, so that the task of identifying elements is not restricted to one particular identification method but support all different locators. While automating a scenario, the identification method or locator that best suits the purpose and is easier and stable to identify a element can be used to locate an element.
Tips for selecting locators:
- ID's are the safest locators as they are unique in DOM, as long as they are stable and not generated automatically or which has a possibility of change unexpectedly.
- Locator should match one element uniquely in DOM and should not return multiple elements which are not desirable or returning elements with which you do not intend to perform operation on.
- For CssSelector and XPath the information used to locate the element should not be changing frequently.
- Device a strategy for CssSelector or XPath by finding pattern in DOM you are working on and construct them intelligently.
- Always keep in mind to make the locators as robust as possible with requiring minimum change or no change going forward.
Final Thought:
Selenium locators are backbone of automation. It has to be thoughtfully selected based on how stable the identification can be based on selected locator method and incur minimum maintenance going forward. CSS Selector and XPath can identify dynamic elements and can help locating any given element in DOM making use of a pattern and combination of different attributes in DOM while others have more direct approach.
https://saglamproxy.com
ReplyDeletemetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
XH6K