LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

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

Answers


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;
import org.openqa.selenium.chrome.ChromeDriver;
public class ClassNameLocator {
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
 WebDriver driver=new ChromeDriver();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 driver.manage().window().maximize();
 //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 
 driver.findElement(By.className("_14fdu48d")).click();
 //Locating the date 12th June for check-in field
 driver.findElement(By.className("_1wh4xpp1")).click();
 //closing the driver
 driver.quit();
 }
}

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.

(more..)

Up Vote Down Vote 0 Votes
Posted 3 months ago