×

WebDriver-Class Name Locator

WebDriver-Class Name Locator

In this tutorial, we will learn about the Class name locator of selenium WebDriver.

The Class Name locator is used to identify any element in UI based on the class backend attribute.

The Syntax of class name locator is as below:

driver.findElement(By.className(“value of class attribute”));

Let us see one sample test case in which we will try to identify the web element with the help of class name locators and automate the following scenarios:

Steps Actions Input Expected Result
1. Open the Google Chrome browser.   The Google Chrome browser should be opened.
2. Navigate to the Yahoo sign-up page. https://login.yahoo.com/account/create?done=https%3A%2F%2Flogin. yahoo.com%2Faccount%2Fpersonalinfo&specId=yidReg The Yahoo sign-up page must be displayed.
3. Identify the first-name text box and pass the value. Firstname=admin The first-name text box should be identified, and the value should be passed.
4. Identify the last-name text box and pass the value. Last Name=admin The last-name text box should be identified, and the value should be passed.
5. Close the Browser.   The Browser should be closed.
  • Firstly we have to Launch Eclipse and open the existing test suite new_test, which we have created in earlier sessions of the WebDriver tutorial.
  • Then right-click on the src folder and create a new Class File from New ? Class.
right-click on the src folder
  • And give your Class name as Class_name and click on the Finish button.
Class name as Class_name

Step1:

  • To access the Google Chrome Browser, first, we need to download the Google Chrome driver and set the system property for the Chrome driver.
  • We have already discussed this in previous sessions of the tutorial.
  • And you can also refer given link "Working with Chrome browser” for a better understanding that how we download it and set System property for Chrome driver.
// set the system property for chrome browser
System.setProperty("webdriver.chrome.driver", "C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//create the object for chrome driver
WebDriver driver = new ChromeDriver();   

Step2:

After that, we will move to our second step, which navigates us to the given URL.

Here the sample code,

//navigate to the URL
 driver.get("https://login.yahoo.com/account/create?done=https%3A%2F%2Flogin.yahoo.com%2Faccount%2Fpersonalinfo&specId=yidReg"); 

Step3:

 Now we are trying to locate the first-name web element by using the value of its class name attribute.

  • Right-click on the first-name text box field and select Inspect element to identify the web element which is as shown below:
 identify the web element
  • And see that the Html code belongs to the first-name text box field got highlighted which is as shown in the below snapshot:
Html code belongs to the first-name text box
  • And notice that the above highlighted Html code has a class attribute.
  • We are using the class value as a class name locator value for identifying the first-name text box field on the particular web page.

Here the sample code for first-name text box:

//identify the first-name text box using class name locator and pass the value
driver.findElement(By.className("ureg-fname")).sendKeys("admin");
Thread.sleep(2000);
System.out.println("first name is entered"); 

Step4:

  • To locate our next web element, right-click on the last-name text box field and select Inspect option to identify the web element which is as shown below:
select Inspect option to identify
  • Notice that the Html code belongs to the last-name text box field got highlighted which is as shown in the below snapshot:
Notice that the Html code belongs to the last-name text box field got highlighted
  • And notice the above highlighted Html code has a class attribute
  • We are using the class value as a class name locator for identifying the last-name text box field on the particular web page.

Here the sample code:

//identify the last-name text box using class name locator and pass the value
 driver.findElement(By.className("ureg-lname")).sendKeys("admin");
 Thread.sleep(2000);
 System.out.println("last name value is entered"); 

Step5:

In the last step of our sample test case, we will close the existing browser,

The sample code for closing the browser,

//Close the browser
driver.close();

Our final test script will look like this:

package testpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Class_name {
public static void main(String[] args) throws InterruptedException { 
 //set the system property
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
 //creating the object for Chrome driver
 WebDriver driver = new ChromeDriver();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
 driver.manage().window().maximize(); 
 //Navigate to Yahoo sign-up page driver.get("https://login.yahoo.com/account/create?done=https%3A%2F%2Flogin.yahoo.com%2Faccount%2Fpersonalinfo&specId=yidReg");
 //identify the first-name text box using class name locator and pass the value
 driver.findElement(By.className("ureg-fname")).sendKeys("admin");
 Thread.sleep(2000);
 System.out.println("first name value is entered");
 //identify the last-name text box using class name locator and pass the value 
 driver.findElement(By.className("ureg-lname")).sendKeys("admin");
 Thread.sleep(2000);
 System.out.println("last name value is entered");
  //closing the browser 
 driver.close();
 }
 } 
  • To run the code in Eclipse, we have to right-click on the code and then select Run as ? Java Application.
  • The above test script will launch the Google Chrome browser and automate all the test scenarios. 
test script will launch the Google Chrome browser

 

 


Related Topics

Selenium WebDriver installation

Installation Steps of Selenium WebDriver In this tutorial, we will see the process of selenium webdriver installation. Selenium WebDriver installation process is done in four Stages: Java Installation (version 8 or higher version) Eclipse...

4 minutes read.

Selenium-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes read.

WebDriver Interface | Class Diagram

WebDriver interface/class diagram: The interface of WebDriver includes the following stages, which is as follows: Search Context Web driver Remote web driver Driver class Search Context  The first interface of the web driver class is search Context,...

2 minutes read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

CSS Selector: ID in Selenium IDE

CSS Selector: ID The id attribute is used to identify the web element uniquely in the web pages. Syntax of ID Attribute:  [HTML tag][#][Value of ID attribute] The hash sign (#): it is mandatory while using the...

1 minute read.

Characteristics of Selenium IDE

Characteristics of Selenium IDE Selenium IDE is separated into different parts, each having their features and functionalities. The seven various components of Selenium IDE are as follows: MENU BAR: The menu bar is...

3 minutes read.

Selenium IDE Tutorial for Beginners

Selenium IDE Introduction Selenium IDE stands for Integrated Development Environment. Selenium-IDE is released in 2006 and developed by SHINYA [JAPAN] THOUGHTWORK Company. Selenium IDE implemented as a Firefox plug-in, and concedes the record, edit, and debug tests....

2 minutes read.

Selenium WebDriver-CSS Selector: Tag Class and Attribute

Selenium WebDriver-CSS Selector: Tag, Class, and Attribute We will locate the web element with the help of Tag, class, and attribute CSS selector locator in this section of the tutorial. Where Tag,...

5 minutes read.

Advantages and Disadvantages of Selenium-IDE

Advantages and Disadvantages of Selenium-IDE Advantages of Selenium IDE: UI based record and playback tool. Easy to understand & developed a test script. Selenium-IDE test script export into web driver code & Selenium–RC It is...

1 minute read.

Selenium WebDriver- Radio button handling

Selenium WebDriver- Radio button handling In this tutorial, we will be learning about handling the radio button in selenium WebDriver. Let us take one example to give you a better understanding of...

3 minutes read.

Selenium WebDriver Tutorial

Seleniun WebDriver Introduction Selenium WebDriver is the essential tool of Selenium Tool Suite because it can directly communicate the browser without any server.  Selenium WebDriver is a browser automation framework that accepts...

3 minutes read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

WebDriver-Tag Name Locator

Selenium WebDriver-Tag Name Locator The tag name locator is used along with the findElements() method to identify multiple similar elements in the UI. It is used to locate the web element with...

3 minutes read.

Selenium WebDriver-CSS Selector: Tag and Class

Selenium WebDriver-CSS Selector: Tag and Class We will locate the web element with the help of tag and class CSS selector in this section of the tutorials Where Tag and class are...

4 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

Selenium WebDriver-CSS Selector: Tag and Attribute

Selenium WebDriver-CSS Selector: Tag and Attribute We will locate the web element with the help of Tag and attribute CSS selector in this section of the tutorial. Where Tag and attribute are...

5 minutes read.

Drag and Drop Event Handling

Drag and Drop Event Handling: In this tutorial, we will learn how to handle the drag and drop event in selenium WebDriver using the action class. Before going further in this tutorial...

6 minutes read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes read.

Verification Controls Methods in Selenium WebDriver

Verification Controls Methods  The verification controls methods are used to check whether the web element is displayed, enabled, and selected. Some of the most commonly used verification controls are as follow: Verification controls Description Return...

4 minutes read.

WebDriver-Class Name Locator

WebDriver-Class Name Locator In this tutorial, we will learn about the Class name locator of selenium WebDriver. The Class Name locator is used to identify any element in UI based on the...

4 minutes read.