• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Selenium » Free Selenium Tutorial » Class Name Locator In Selenium

Class Name Locator In Selenium

Last Updated on: August 19, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Class Name Locator
  • When Multiple Elements Have the Same Class Name

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 the Class name locator in selenium and also see how with the class name locator we can find elements on a web page.

To understand this we will try to find out 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 when there is a single 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.

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 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.

    Filed Under: Free Selenium Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers