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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; public class SeleniumMain { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "./chromedriver.exe"); WebDriver driver; ChromeOptions cOptions=new ChromeOptions(); cOptions.addArguments("--start-maximized"); driver=new ChromeDriver(cOptions); driver.get("https://www.societyhive.com/"); WebElement loginRegisterButton=driver.findElement(By.xpath("//button[.='Login / Register']")); loginRegisterButton.click(); WebElement forgotPasswordLink=driver.findElement(By.className("sh-forgotpassword")); forgotPasswordLink.click(); //comment to see the result driver.close(); } } |