0
0
Selenium Javatesting~10 mins

findElement by name in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a browser, navigates to a sample login page, finds the username input field by its name attribute, enters a username, and verifies the input was successful.

Test Code - JUnit with Selenium WebDriver
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.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class FindElementByNameTest {
    private WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
    }

    @Test
    public void testFindElementByName() {
        driver.get("https://example.com/login");
        WebElement usernameInput = driver.findElement(By.name("username"));
        usernameInput.sendKeys("testuser");
        assertEquals("testuser", usernameInput.getAttribute("value"));
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Browser navigates to https://example.com/loginLogin page is loaded with username input field having name='username'-PASS
3Find element by name 'username' using driver.findElement(By.name("username"))Username input field is located on the pageElement found with name attribute 'username'PASS
4Enter text 'testuser' into the username input fieldUsername input field contains text 'testuser'-PASS
5Verify the input field's value attribute equals 'testuser'Username input field value is 'testuser'assertEquals("testuser", usernameInput.getAttribute("value"))PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: The element with name 'username' is not present on the page
Execution Trace Quiz - 3 Questions
Test your understanding
Which Selenium method is used to find the username input field in this test?
Adriver.findElement(By.id("username"))
Bdriver.findElement(By.name("username"))
Cdriver.findElement(By.className("username"))
Ddriver.findElement(By.tagName("input"))
Key Result
Always verify the locator attribute (like 'name') matches exactly what is on the page, and ensure the page is fully loaded before locating elements to avoid NoSuchElementException.