top of page
  • Writer's picturePerfectQA

Locators in Selenium with Example

Updated: 3 days ago

Techniques Of Locator In Selenium Webdrivers

Today we are covering the “Selenium Locators” or “locators in Selenium” ,an important and very potent part of the Selenium web driver.

What is a Selenium Web Driver?

  • Selenium WebDriver is a cluster of open source APIs which is used to automate the testing of a web application.

  • In simple words, selenium web driver is used to check that if a particular web application works as it is meant to. It supports all the popular browsers such as Chrome,Safari,Edge etc.

  • Earlier we have to do this by ourselves and this was very hectic, but now it is very simple and efficient using Selenium.

  • Many components comprise Selenium and make it possible.

  • One of them is “Selenium Locator” or “Locators in selenium”.

What is this Selenium Locator?

  • Selenium needs locators to match the elements of your web application with what it needs to run and use the process.

  • They are like the building blocks of Selenium Driver as without them Selenium would not be able to identify what it needs to interact with and what not. Locators in Selenium, if failed, can even result in script failure.

  • In other words, a good web application requires multiple elements found correctly. For this we have multiple locators listed below-

  1. ID

  2. Name

  3. Linktest

  4. Partial Linktest

  5. Tagname

  6. Classname

  7. CSS Selector

  8. Xpath

Before jumping to what these do,we will see how to find a Document-object Model(DOM).

STEPS TO FIND DOM

Step 1) Open the target application and click on F12 or right click and select inspect.

Step 2)Open the target application and click on F12 or right click and select inspect.

Step 3)A section name as ‘Element’ would automatically open. This is where we hit on elements through. To the left most, a mouse icon can be observed. As you hover on the mouse icon, it would state ’select an element in the page to inspect it’. Click on it and steer to the element you want to locate to.

Step 4)This is it, the selected row in the DOM is the context from where you need to fetch your values from.

This will look like this-

<a href=”https://accounts.example.com/register” target=”_blank”>Free Sign Up</a>

Now you can choose the tagname i.e ‘a’ and text i.e ‘Free Sign Up’ to locate your element.

ID Locator

  • This marks the most common way to locate elements given they are unique to each element in the DOM

  • The World Wide Web Consortium(W3C) states that each element should be unique, making Id locator the most common and easy move.

  • But developers may or may not follow this as many browsers allow bypassing this rule.

  • In those cases, we use other means to locate elements.

Syntax-

<object name>.findelement(By.Id("value of the ID")).provide action which you want to perform.
driver.findElement(By.id("from")).sendKeys("Illinois");

Name Locator

Just like how we use names to identify and locate Humans,Selenium also uses element name to locate them.

They might not be unique to a page,in that case Selenium selects the first element with that name on the page.

If no element by that name is found,then an expectation is raised.

Syntax

driver.findElement(By.name("email"));

Link Text Locator

Elements can also be located by their links as in hyperlinks. In Case of multiple links for the same text, the first one would be selected.

Syntax

linktext=driver.findElement(By.linkText("REGISTER"));

Partial Link Text Locator

  • This is what you can consider a slightly upgraded form of Link Text Locator.

  • If you have a very long link and you just want to use the partial text to locate the link, we can use this.

  • It can also be used to find multiple links with the same partial text.

Syntax

driver.findElement(By.partialLinkText("REGISTER")).click( );

Tag Name Locator

  • As if it is not clear from the name itself , this is used to identify elements using CSS tags like div tag, a tag etc.

  • It can be used to check all the links to see if they are functional or not.It can also be used to see content within a tag.

Syntax

driver.findElements(By.tagName(a));

Class Name Locator

Class Name locator aids in locating elements defined through the class attribute.

Class Name locator accords the element which matches the values specified in the attribute name “class”.

Syntax

driver.findElement(By.classname("inputtext_58mg_5dba_2ph- ")).click();

CSS Selector Locator

CSS Selector is the amalgamation of an element selector and a selector value that identifies the web element within a web page. The amalgamation of an element selector and selector value is known as Selector Pattern.

CSS or Cascading Style Sheets are used to style webpages and thus it becomes a way to locate elements.

CSS Selectors can be located in various formats as below-

  1. Tag and ID

  2. Tag and Class

  3. Tag and Attribute

  4. Tag, class and attribute

  5. Matches(starts with,ends with,contains)

  6. Child Elements

XPath Locator In Selenium

Xpath or XML path is used to identify elements from XML documents. It has path expression along with some conditions.

To locate any element we just have to write a simple XML query/script.

Syntax

//tagname[@attribute='value']

Tag name signifies the tag in the DOM structure you are targeting to.

There are 2 types of Xpaths

  1. Absolute Xpath

  2. Relative Xpath

Difference between Absolute Xpath and Relative Xpath

Absolute XPath begins with the root node or a forward slash (/). The advantage of using absolute is, it recognizes the element rapidly.

The disadvantage is, if anything goes wrong like some other tag added or removed in between, then this path will no longer work.

While

Relative Xpath :- A relative xpath is one where the path starts from the node of your choice — it doesn’t need to start from the root node. It starts with Double Forward Slash(//).

The superiority of using relative xpath is, you don’t need to mention the long xpath, you can start from the middle or in between.

The disadvantage here is, it will take more time in identifying the element as we specify the partial path not (exact path).

Things to keep in mind while using locators in Selenium

There are certain things to keep in mind while using these locators.  Following these will ensure a smooth and error-free experience with these things.

Do not use locators over elements which may change making your locators failure-prone.

Locators should be dependent on one single element rather than multiple ones. Also make sure if you have dependent locators, then they should be less-likely to change.

Locators should only have required information collected.

If we are looking at multiple matches then it should have all the desired selections it needs to have.

Locators should not be used on auto-generated values.

These were the factors to consider while using Locators. They may sound complex for once but become easy if you use them while following the rules.

62 views0 comments

Related Posts

See All
bottom of page