In our previous post, we have seen different locators, how to locate an element using ID and Name. In this post, we are going to another new locator by which we can find the element in a web page that’s is Class Name locator. You can check the implementation of the other locators by following the below links:
Class Name Locator
- Locate element By Using ID Locator.
- Locate element By Using Name Locator.
- Locate element By Using Tag Name Locator.
- Locate element By Using Link Text & Partial Link Text Locator.
- Locate element By Using CSS Selector Locator.
- Locate element By Using XPath Locator.
In this Java Selenium tutorial, we are going to discuss Class name locator in selenium and also see how with the class name locator we can find elements on a web page.
This is also a good alternative for you if your elements contain the class name attribute. But when you try to locate the element on the web page then a common exception you may see that is InvalidSelectorException.
How to Handle InvalidSelectorException?
Let us Take an Example Of Facebook Signup button DOM:
<div class="clearfix"><button type="submit" class="_6j mvm _6wk _6wl _58mi _3ma _6o _6v" name="websubmit" id="u_0_15">Sign Up</button><span class="hidden_elem _58ml" id="u_0_16"><img class="img" src="https://static.xx.fbcdn.net/rsrc.php/v3/yb/r/GsNJNwuI-UM.gif" alt="" width="16" height="11"></span></div>
Let me told you how you could handle such a scenario when you are getting InvalidSelectorException. If you see the class name on the DOM, you can found the class name is not a single name its a compound class name which means there are multiple classes, but those are marked through the spaces. So if any class name has with spaces, then that will be treated as more then one classes.
In our case, the class name contains the value “_6j mvm _6wk _6wl _58mi _3ma _6o _6v” having 8 spaces that means it has 8 different classes. Hence when we try to execute our automation script that time we are getting this error
Exception in thread “main” org.openqa.selenium.InvalidSelectorException: The given selector a:contains(‘_6j mvm _6wk _6wl _58mi _3ma _6o _6v’) is either invalid or does not result in a WebElement. The following error occurred:
To Solve such type of errors, you need to use the CSS Selector or Xpath with the class name attribute.
https://dzone.com/articles/selenium-java-tutorial-class-name-locator-in-selen
When Multiple Elements Have the Same Class Name
In the above example, I guess you know how to deal when there is multiple class name present in a single class name attribute. But have you think about such a scenario where multiple elements have the same class name. So, to handle such type of situation you need to use of the findElements keyword with that you can get all the matching elements and from there you can store all the matching elements in a list and iterate them by using any iterator and find the required element with the help of the index.
Doing such things, it’s better to find out another alternative method because there are more chances of getting errors in this process.
Add the below examples
I hope with the help of this tutorial you will be getting a good understanding of how to use the class name locators in selenium effectively and also the most common errors across the implementation of the class name locator. Also, we have discussed how to handle when multiple class shares the same class name scenario.
Still, if you have any doubts, then you can comment in the comment section, and we are happy to help you doubts clear.
Leave a Reply