LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

How can I locate an element when I have multiple elements sharing the same class name?

Can anyone please tell me how do i locate an element when i have multiple elements sharing the same class name.

Up Vote Down Vote 0 Votes
Posted 3 months ago

Answers


You can simply achieve this by using the findElements keyword. All you need to do is, locate all elements with that class name using the findElements keyword and iterate through the required element class name via index. Having said so, I would rather suggest looking into an alternative method of locating that element rather than this. As its tendency of breaking will be pretty high and may lead to errors.

Example For Class Name Locator In Selenium For Multiple Elements With A Similar Class Name

Let’s consider the below example, highlighting the scenario above. In this case, we are considering the LinkedIn sign-up page, where all fields share the same class name. In this case, we need to note two important things:

  • If no, the index is defined, then by default selenium selects the first element it encounters with that class name. In the code snippet below, it locates the first element which is the first name:
import java.util.concurrent.TimeUnit;
import http://org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ClassNameLocator {
 public static void main(String[] args) throws InterruptedException {
 // 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 linkedin sign up home page
 driver.get("Sign Up");
 //Locating by firstname via class name 
 driver.findElement(By.className("cell-body-textinput")).sendKeys("Sadhvi");
 //closing the driver
 driver.quit();
 }
}

Note: Take a look at the class name, which in this case is cell-body-textinput, since it is not marked with spaces hence it is considered as a single class.

  • Locating the different elements with the same class name using index. The referenced snippet below:

import java.util.List;
import java.util.concurrent.TimeUnit;
import http://org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ClassNameLocator {
 public static void main(String[] args) throws InterruptedException {
 // 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("Sign Up");
 //Locating by firstname via class name 
 List<webelement> signUpForm=driver.findElements(By.className("cell-body-textinput"));
 //finding the number of elments with the same class name
 int size=signUpForm.size();
 System.out.print(size);
 //locating the first name locator
 signUpForm.get(0).sendKeys("Sadhvi");
 //locating the last name locator
 signUpForm.get(1).sendKeys("Singh");
 //locating the email locator
 signUpForm.get(2).sendKeys("sadhvisingh24@gmail.com");
 //locating the password locator
 signUpForm.get(3).sendKeys("password");
 //closing the driver
 //driver.quit();
 }
}
</webelement>

Console output:

Up Vote Down Vote 0 Votes
Posted 3 months ago
210 Views
1 Answer
3 months ago