0
0
Selenium Javatesting~15 mins

findElement by className in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify login button is clickable using className locator
Preconditions (2)
Step 1: Locate the login button using its class name 'btn-login'
Step 2: Click the login button
Step 3: Verify that the URL changes to 'https://example.com/dashboard'
✅ Expected Result: After clicking the login button located by class name, the user is redirected to the dashboard page with URL 'https://example.com/dashboard'
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the login button is found using By.className
Verify the login button is clickable
Verify the URL after clicking is 'https://example.com/dashboard'
Best Practices:
Use explicit waits to wait for the login button to be clickable
Use By.className locator only for single class names, not multiple classes
Use assertions from a testing framework like TestNG or JUnit
Close the browser after test execution
Automated Solution
Selenium Java
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.time.Duration;

public class LoginButtonTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeClass
    public void setUp() {
        // Set path to chromedriver if needed
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get("https://example.com/login");
    }

    @Test
    public void testLoginButtonClickableByClassName() {
        // Wait until the login button is clickable
        WebElement loginButton = wait.until(
            ExpectedConditions.elementToBeClickable(By.className("btn-login"))
        );

        // Assert the button is displayed
        Assert.assertTrue(loginButton.isDisplayed(), "Login button should be visible");

        // Click the login button
        loginButton.click();

        // Wait until URL changes to dashboard
        wait.until(ExpectedConditions.urlToBe("https://example.com/dashboard"));

        // Assert the URL is correct
        Assert.assertEquals(driver.getCurrentUrl(), "https://example.com/dashboard", "URL should be dashboard after login");
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

This test class uses Selenium WebDriver with Java and TestNG for assertions.

setUp() opens the browser and navigates to the login page.

The test testLoginButtonClickableByClassName waits explicitly for the login button located by By.className("btn-login") to be clickable. This avoids timing issues.

It asserts the button is visible, then clicks it.

After clicking, it waits for the URL to change to the dashboard URL and asserts the URL is exactly as expected.

tearDown() closes the browser to clean up.

This structure follows best practices: explicit waits, proper locator usage, and clean test lifecycle.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using By.className with multiple class names separated by spaces', 'why_bad': 'By.className only accepts a single class name, not multiple classes separated by spaces. This causes NoSuchElementException.', 'correct_approach': 'Use a single class name with By.className or use By.cssSelector with multiple classes, e.g. By.cssSelector(".class1.class2")'}
Not using explicit waits before interacting with elements
Hardcoding URLs without waiting for page load or URL change
Bonus Challenge

Now add data-driven testing with 3 different class names for buttons to verify each is clickable and leads to correct URLs

Show Hint