Careeroza
What is a Locator
Finding an Element
A locator tells Selenium how to find an element on the page. You pass a By object to driver.findElement() and get back a WebElement you can click or type into.
<form id="login-form">
<input type="email" id="email" name="email" class="form-input" placeholder="Email">
<input type="password" id="password" name="password" class="form-input">
<button type="submit" id="login-btn" class="btn btn-primary">Login</button>
<a href="/forgot-password">Forgot your password?</a>
</form>The Eight Strategies
| Locator | Example for the form above |
|---|---|
By.id | By.id("email") |
By.name | By.name("password") |
By.className | By.className("btn-primary") |
By.tagName | By.tagName("button") |
By.linkText | By.linkText("Forgot your password?") |
By.partialLinkText | By.partialLinkText("Forgot") |
By.cssSelector | By.cssSelector("#login-form .btn-primary") |
By.xpath | By.xpath("//button[text()='Login']") |