×

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 Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 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.

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-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute 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.

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.

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 WebDriver - Google Chrome Browser

Selenium WebDriver - Google Chrome Browser In this tutorial, we will learn how to run our Selenium Test Scripts in the Google Chrome Browser. To implement the WebDriver protocol with the help...

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.

Selenium WebDriver-CSS Selector locator

Selenium WebDriver-CSS Selector locator A CSS selector is used to identify the web element based on their “# id” and “. Class”, Html tags, and attributes. A CSS stands for Cascading Style...

1 minute 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.

Link Text Locator in Selenium IDE

LINK TEXT Link textlocator is used to identify only the weblink element based on the name of the link. Whenever multiple links are there, it will not work; it will work only...

1 minute read.

Drawback or Limitations of Selenium

Drawback or Limitations of Selenium Followings are the Drawback or limitations of Selenium:- Drawback /LimitationsDescriptionDesktop applications cannot be automated by Selenium.To test the desktop application, we have tools like Test Complete, Winnium, and...

2 minutes read.

Xpath locator in Selenium IDE

X PATH LOCATOR: Xpath is used to identify the element using any attribute or visible test. Syntax for X PATH:- //html tag [@attribute=’value’] e.g. - //div [@class=’label’]                    OR //html tag [text () =’Visible Text’] e.g.-//div [text...

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.

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.

Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc. Some of the most commonly used Browser controls commands for Selenium WebDriver...

6 minutes read.

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

Selenium WebDriver - Navigation Controls Commands

Navigation Controls Commands in Selenium WebDriver WebDriver is used to hold the browser history and perform browser navigation operations like, back, forward, and refresh. Whenever a WebDriver launches a browser, all...

5 minutes read.

CSS Selector: Attribute in Selenium IDE

CSS Selector: Attribute Locating the attribute of a CSS selector, we would access the Password textbox, which is present in the login form of Facebook. The Password textbox has a type attribute...

1 minute read.