Locators in Selenium – Before going ahead, I assume you have already gone through the installation of FireBug and FirePath. If not, check out the link mentioned below:
We use FireBug and FirePath to identify the Locators in Selenium
Hope you have already installed Selenium WebDriver. If not go through the below mentioned link to download and install Selenium WebDriver.
Selenium identifies the elements to be worked on using the following locators.
Different types of Locators in Selenium are as follows:
Locating elements in WebDriver is done by using the method “findElement(By.locator())“.
We use above mentioned method to locate elements but the locator() part will be replaced with the locator names. Mentioned few examples below for reference.
Here is the link to the post “Difference between findElement and findElements methods”
Let’s see each of the Locators in Selenium in detail:
ID’s are unique for each element so it is common way to locate elements using ID Locator. As per W3C, ID’s are supposed to be unique on a page and it makes ID’s are the most reliable locator. ID locators are the fastest and safest locators out of all locators.
id = id of the element
We sometimes use Name locator to identify the elements on our webpage. Locating elements using Name is same as locating elements using ID locator.
These are not unique on a page. If there are multiple elements with the same Name locator then the first element on the page is selected. Test may fail, if another element with the same Name locator is present on the web page or added by the developers in the later stages.
Name = Name of the element
Class Name Locator:
Class Name locator gives the element which matches the values specified in the attribute name “class”.
Tag Name Locator:
Tag Name locator is used to find the elements matching the specified Tag Name. It is very helpful when we want to extract the content within a Tag.
findElement(By.tagName("HTML Tag Name"))
Link Text Locator:
If there are multiple elements with the same link text then the first one will be selected. This Link Text locator works only on links (hyperlinks) so it is called as Link Text locator.
Partial Link Text:
In some situations, we may need to find links by a portion of the text in a Link Text element. it contains. In such situations, we use Partial Link Text to locate elements.
CSS Selector Locator:
There is a debate on the performance between CSS Locator and XPath locator and the debate on the performance of CSS and XPath locator is out of scope of this post. Most of the automation testers believe that using CSS selector makes the execution of script faster compared to XPath locator. This locator is always the best way to locate elements on the page.
Following are the some of the mainly used formats of CSS Selectors.
- Tag and ID1findElement(By.cssSelector(tag#id))
- Tag and Class1findElement(By.cssSelector(tag.class))
- Tag and Attribute1findElement(By.cssSelector(tag[attribute=value]))
- Tag, Class and Attribute1findElement(By.cssSelector(tag.class[attribute=value]))
XPath is designed to allow the navigation of XML documents, with the purpose of selecting individual elements, attributes, or some other part of an XML document for specific processing. XPath produces reliable locators but in performance wise it is slower (especially in IE older versions) compared to CSS Selector.