In our previous post, we saw different locators for locating an element using ID and Name. In this post, we are going to another new locator by which we can find the element on a web page: the 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 will discuss the Class name locator in Selenium and also see how we can find elements on a web page with the class name locator.
To understand this, we will try to find a web element using the Name locator, and after finding that element, we will enter some text in the textbox.
package com.selenium.practice.locator; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ClassName_Locator { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/08/class-name-locator.html"); Thread.sleep(10000); WebElement nameTextbox=driver.findElement(By.name("email")); //Entering A vlaue In the Text Box nameTextbox.sendKeys("SoftwareTestingo"); Thread.sleep(5000); driver.close(); } }
When Multiple Elements Have the Same Class Name
In the above example, I guess you know how to deal with when there is a single class name present in a single class name attribute. But have you considered a scenario where multiple elements have the same class name? So, to handle such a situation, you need to use 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 another alternative method because there are more chances of getting errors in this process.
package com.selenium.practice.locator; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class MultipleElement_SameClassName { public static void main(String[] args) throws InterruptedException { WebDriver driver; System.setProperty("webdriver.chrome.driver","Path Of Browser Driver"); driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://softwaretestingo.blogspot.com/2020/08/multiple-element-have-same-classname.html"); Thread.sleep(10000); List<WebElement> list=driver.findElements(By.className("inputtext")); int size=list.size(); System.out.println("There are "+size+" elements which have the same class name"); list.get(0).sendKeys("SoftwareTestingo"); list.get(1).sendKeys("Admin"); list.get(2).sendKeys("Selenium"); Thread.sleep(5000); driver.findElement(By.id("submitbtn")).click(); } }
I hope that with the help of this tutorial, you will understand how to use the class name locators in Selenium effectively and the most common errors across the implementation of the class name locator. Also, we have discussed how to handle situations where multiple classes share the same class name.
Still, if you have any doubts, you can comment in the comment section, and we are happy to help clear them up.