×

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-IDE Installation in Chrome

Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Google Chrome browser in your system. However, you can...

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

Selenium IDE Commands

Selenium –IDE Commands (SELENESE): Selenese commands are the set of commands used in Selenium IDE. Selenese command has two parameters target and value. Selenese command tests the existence of UI elements based on their HTML tags,...

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

Selenium Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

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

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.

Selenium WebDriver Commands

Selenium WebDriver Commands: As we learn previously in the Selenium-IDE tutorial, Selenium commands are the set of commands that are used to run a Selenium test script. But whereas, Selenium WebDriver had...

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.

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.

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

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

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

1 minute read.

Selenium Tool Suite

The selenium tool suite is a combination of multiple software tools, and the entire tools having a different approach to support automation testing. Selenium test tool suites are as follows- Selenium...

2 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 – Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.

Selenium WebDriver- Checkbox handling

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

4 minutes read.