LambdaTest Community

Find answers, support, and inspiration from other users

LambdaTest Community

What is Class Name Locator in Selenium? How to get started with it?

Can anyone please provide me some knowledge regarding what is Class Name Locator in Selenium and how do i get started with it.

Up Vote Down Vote 0 Votes
Posted 3 months ago


Getting Started With Class Name Locator In Selenium With Example

Next, in this Selenium Java tutorial, we will consider the scenario of Airbnb, where we intend to locate the ‘Where’ field in the search form of Airbnb homepage via class name. Below is a screenshot of Airbnb page where we inspect the ‘Where’ field in the form. image:

In-order to use Class name locator in Selenium we need to use the below syntax: findElement(By.className("_up0kwni "))

Now, let’s look into the code for finding elements by Class name in locators:

import java.util.concurrent.TimeUnit;
import http://org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class ClassNameLocator {
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 System.setProperty("", ".\\ChromeDriver\\chromedriver.exe");
 WebDriver driver=new ChromeDriver();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 //Opening the air bnb home page
 driver.get("Vacation Rentals, Homes, Experiences & Places - Airbnb");
 //Locating location field for the search form via class name 
 driver.findElement(By.className("_up0kwni")).sendKeys("Goa", Keys.ENTER);
 //Locating check-in field for the search form via class name 
 //Locating the date 12th June for check-in field
 //closing the driver

Pretty simple, isn’t it? In the upcoming session of this Java Selenium tutorial we will learn how to handle a common exception for class name locator in Selenium.


Up Vote Down Vote 0 Votes
Posted 3 months ago