Home > Java > Selenium WebDriver Locator: Class Name

Selenium WebDriver Locator: Class Name

This post was most recently updated on June 14th, 2019

Locator Type: Class Name

  • Class attribute in HTML element used to apply style or select web element(s) JavaScript functions. 
  • One class attribute could contain multiple classes separated by space and it is called as composite class. 
  • Class attribute need not to be unique like id. 

Java code to find web element using class attribute value. 

WebElement wElement=driver.findElement(By.className("class_Attribute_Value"));

Steps to find class attribute of web element.

  • Open url “https://www.societyhive.com/” in chrome browser.
  • Click F12 to open DevTools in chrome browser.
  • Click on elements tab and press shortcut key Ctrl+F.
  • Inspect element by using Ctrl+Shift+C or by clicking on arrow icon in devTools.
  • Enter text “sh-forgotpassword” and copy class attribute value.

WebElement phoneNumber=driver.findElement(By.className("sh-forgotpassword")); 

Sample Program:

Selenium Tutorial >>

This Article is TAGGED in , , , . BOOKMARK THE permalink.

Avatar photo
Neeraj Vishwakarma
Senior QA Engineer Skills: Automation Testing, Manual Testing, STLC, Java, Selenium Webdriver, Protractor, Database Testing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">