Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Selenium Webdriver Locating Strategies By Class Name
By class name can be used as a locating strategies in Selenium webdriver. We can identify an element utilizing class attribute with locators like class name, css and xpath. To locate webelement with css, the syntax is tagname[class='value'] and the method to be used is By.cssSelector.
To locate webelement with xpath, the syntax is //tagname[@class='value']. TThen, we have to use the method By.xpath to locate it. To locate an element with a locator class name, we have to use the By.className method.
Let us look at the html code of web element having class attribute −

Syntax
WebElement elm = driver. findElement(By.className("input__input"));
WebElement p = driver.findElement(By.xpath("//input[@class = ' input__input']"));
WebElement t = driver. findElement(By.cssSelector("input[class=' input__input']"));
Example
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class ClsNameStrategy{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver",
"C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//implicit wait
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//URL launch
driver.get("https://www.linkedin.com/");
// identify element class name
WebElement elm = driver.findElement(By.className("input__input"));
elm.sendKeys("abc@gmail.com");
//identify with cssSelector
WebElement c= driver.findElement(By.cssSelector("input[class='input__input']"));
String str = c.getAttribute("value");
System.out.println("Value entered is : " + str);
//identify with xpath
WebElement x = driver.
findElement(By.xpath("//input[@class='input__input']"));
x.clear();
driver.close();
}
}
Output

Advertisements
