Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

How To Locate Web Element Which Has Multiple Class Names

Posted on 02/19/2025 By admin

A web element of a web page may belong to multiple classes. These classes may represent a class in style sheet or can be used for DOM manipulation. All belonging class names of a web element are mentioned under “class” attribute separated by a space. For example :-


This element belongs to class demo1
This element belongs to class demo1 and dmeo2
This element belongs to class demo1, demo2 and demo3

We will see how can we locate these type of elements in Selenium WebDriver.

If you inspect “From” text box at Yatra then you will find it has multiple classes which is also called compound class names.

Let’s use className locator and see what happens.

package SpecialConcepts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ElementWithMultipleClassNames {

        @Test
        public void multipleClassnameWithClassNameLocator()
        {
                WebDriverManager.chromedriver().setup();
                WebDriver driver=new ChromeDriver(); 
                driver.get("https://www.yatra.com/");
                driver.findElement(By.className("required_field cityPadRight ac_input origin_ac")).sendKeys("DELHI");
        
        }
}

When we pass all class names in className attribute it has thrown above exception because class attribute has compound class names i.e. required_field, cityPadRight, ac_input and origin_ac. This is not permitted by classname locator in Selenium WebDriver.

You can use classname locator with individual class names as below:

// It will match all web elements which has class name "required_field" but returns first match
driver.findElement(By.className("required_field")); 
// It will match all web elements which has class name "cityPadRight" but returns first match
driver.findElement(By.className("cityPadRight"));

You can use findElements() with index or Xpath with index to get exact web element. Using index makes locator flaky as it may change based on addition or remove or modification of elements in DOM.

We can use xpath and cssSelector to locate web elements with multiple or compound class names. Use “class” as attribute and its complete value as it is in quotations for XPath and CSS Selector. Example is shown as below :-

driver.findElement(By.xpath("//input[@class='required_field cityPadRight ac_input origin_ac']")).sendKeys("DELHI");
driver.findElement(By.cssSelector("input[class='required_field cityPadRight ac_input origin_ac']")).sendKeys("DELHI");

But cssselector has multiple ways to locate web element with compound class names.

You can combine all class names with dot (.) and prefix by node i.e. tagName.classname1.classname2.classname3. For example Above node is “input” so another css selector can be written as :-

driver.findElement(By.cssSelector(“input.required_field.cityPadRight.ac_input.origin_ac”)).sendKeys(“DELHI”);

If you do not want to specify node name or want it to be any tag name then combine all class names with dot (.) and append a dot (.) in beginning which generally represents a class value in cssSelector i.e. .classname1.classname2.classname3

driver.findElement(By.cssSelector(“.required_field.cityPadRight.ac_input.origin_ac”)).sendKeys(“DELHI”);

package SpecialConcepts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ElementWithMultipleClassNames {

        @Test
        public void multipleClassnameWithClassNameLocator()
        {
                WebDriverManager.chromedriver().setup();
                WebDriver driver=new ChromeDriver(); 
                driver.get("https://www.yatra.com/");
                driver.findElement(By.className("required_field cityPadRight ac_input origin_ac")).sendKeys("DELHI");
        
        }
        
        @Test
        public void multipleClassnameWithXPathLocator()
        {
                WebDriverManager.chromedriver().setup();
                WebDriver driver=new ChromeDriver(); 
                driver.get("https://www.yatra.com/");
                driver.findElement(By.xpath("//input[@class='required_field cityPadRight ac_input origin_ac']")).sendKeys("DELHI");
        
        }
        
        @Test
        public void multipleClassnameWithCssSelectorLocator()
        {
                WebDriverManager.chromedriver().setup();
                WebDriver driver=new ChromeDriver(); 
                driver.get("https://www.yatra.com/");
                driver.findElement(By.cssSelector("input[class='required_field cityPadRight ac_input origin_ac']")).sendKeys("DELHI");
        
        }
        
        @Test
        public void multipleClassnameWithCssSelectorLocatorCombiningClassWithNode()
        {
                WebDriverManager.chromedriver().setup();
                WebDriver driver=new ChromeDriver(); 
                driver.get("https://www.yatra.com/");
                // Combine all class names with dot and append tag name i.e. tagName.classname1.classname2.classname3
                driver.findElement(By.cssSelector("input.required_field.cityPadRight.ac_input.origin_ac']")).sendKeys("DELHI");
        
        }
        
        @Test
        public void multipleClassnameWithCssSelectorLocatorCombiningClassWithoutNode()
        {
                WebDriverManager.chromedriver().setup();
                WebDriver driver=new ChromeDriver(); 
                driver.get("https://www.yatra.com/");
                // Combine all class names with dot and append a dot at starting i.e. .classname1.classname2.classname3
                driver.findElement(By.cssSelector(".required_field.cityPadRight.ac_input.origin_ac']")).sendKeys("DELHI");
        
        }
}

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: threadlocal in selenium webdriver
Next Post: Postman Tutorial Part 12 – Understand Environment & Variables in Postman

Related Posts

identify angular Uncategorized
REST Assured Tutorial 73 – How to ignore node/s for JSON comparison in JSONassert Uncategorized
Rūto – Get Started & Save Time in Finding Locators Uncategorized
REST Assured Tutorial 35 – De-Serialization – JSON Object To Java Object Using Gson API Uncategorized
REST Assured Tutorial 44 – Fetch Value From JSON Object Using JsonNode – Jackson – get() & path() Methods Uncategorized
getAttribute() method in Selenium WebDriver – Why, What and How to use? Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark