0
0
Selenium Javatesting~15 mins

findElement by name in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify login button is clickable using findElement by name
Preconditions (2)
Step 1: Open the browser and navigate to http://example.com/login
Step 2: Locate the login button using its name attribute 'loginButton'
Step 3: Click the login button
✅ Expected Result: The login button is found by its name and clicked successfully without errors
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify that the login button is displayed before clicking
Verify that the login button is enabled before clicking
Best Practices:
Use explicit waits to wait for the login button to be visible and clickable
Use By.name locator to find the element
Use try-catch to handle NoSuchElementException
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 java.time.Duration;

public class FindElementByNameTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        try {
            driver.get("http://example.com/login");

            // Wait until the login button is visible
            WebElement loginButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("loginButton")));

            // Assert the button is displayed
            if (!loginButton.isDisplayed()) {
                throw new AssertionError("Login button is not displayed");
            }

            // Assert the button is enabled
            if (!loginButton.isEnabled()) {
                throw new AssertionError("Login button is not enabled");
            }

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

            System.out.println("Test passed: Login button found and clicked successfully.");
        } catch (Exception e) {
            System.out.println("Test failed: " + e.getMessage());
        } finally {
            driver.quit();
        }
    }
}

This test script uses Selenium WebDriver with Java to automate the manual test case.

First, it sets up the ChromeDriver and opens the browser to the login page URL.

It uses an explicit wait to wait until the login button with the name='loginButton' is visible on the page.

Then it checks if the button is displayed and enabled before clicking it. If either check fails, it throws an assertion error.

Finally, it clicks the button and prints a success message. The browser is closed in the finally block to ensure cleanup.

This approach follows best practices by using explicit waits, proper locators, and error handling.

Common Mistakes - 4 Pitfalls
{'mistake': 'Using driver.findElement(By.id("loginButton")) instead of By.name', 'why_bad': "The locator does not match the element's attribute, so the element won't be found.", 'correct_approach': 'Use driver.findElement(By.name("loginButton")) to match the element\'s name attribute.'}
Not using explicit waits and immediately calling findElement
Not checking if the element is displayed or enabled before clicking
Not closing the browser after test execution
Bonus Challenge

Now add data-driven testing with 3 different name attributes for buttons to verify each is clickable.

Show Hint