ClassName locator helps in locating element through the class attribute in Selenium.  These are also not unique on DOM. And if there is no DOM element available with the given className, then NoSuchElementException is raised in that case.


Syntax: WebElement element = driver.findElement(By.className(<CLASSNAME>));


Let’s take an example by locating “side menu” in “” page.

Step1: Open” in browser

Step2: Hover your mouse to side-menu

Step3: Right click on the mouse and do inspect element

ClassName Locator Example

element = driver.findElement(By.className("nav"));