About this lesson
Selenium Java findElement with By.id: locate an element by its id
Selenium Java AssignmentsBeginnerLocators
How it works
By.id("value") matches the element whose id attribute equals that value. You pass it to driver.findElement(), which returns a WebElement you can read or act on. Selenium sends it to the browser as a CSS selector built from the id, which is why a failed lookup reports the css selector method in its error message.
HTML expects an id to be unique, and that is why id is usually the most dependable locator. The Text Fields page breaks the rule on purpose: field 1 and field 6 both carry the id firstName, and findElement() quietly returns the first one in document order.
If nothing matches, findElement() does not return null. It throws NoSuchElementException. When you only want to test whether something exists, call findElements() instead; it returns an empty List<WebElement> rather than throwing.
About the assignment
The Text Fields page has a last-name box that shows grey hint text before you type. You will locate that box by its id and read the hint, which lives in the field's placeholder attribute rather than in its visible text.
What you will do
- The page is already open. Build an id locator for
lastNameWithPlaceholderwithBy.id(). - Pass it to
driver.findElement()to get theWebElement. - Read the
placeholderattribute from that element. - Print the value after the label
Placeholder:.
Common mistakes
Calling getText() on an input
An
inputhas no inner text, sogetText()returns an empty string for this field. The hint is an attribute; read it withgetAttribute()orgetDomAttribute().Generated ids
Some frameworks create ids such as
ember412orreact-select-3-inputthat change on every build or page load. A locator built on one passes today and fails tomorrow withNoSuchElementException; pick a stable attribute instead.Element inside an iframe
The driver only searches the current frame. An element inside an
iframeis not found by id until you switch into it withdriver.switchTo().frame(), which lessons 44 to 47 cover.
In a real test suite
In a page object class, id locators are usually stored once as By fields and reused by every method, so a changed id is fixed in one place. To read an attribute exactly as written in the HTML, use getDomAttribute().
Not every element has an id. Lesson 5 locates the read-only field on this same page through its name attribute instead, the usual fallback for form controls.
Related lessons
- Find Element by NameThe next attribute to try when a form field has no id.
- CSS Selectors - BasicsWrite the same lookup as a CSS
#idselector and combine it with a tag name. - Get Attribute Value with getAttribute()More on
getAttribute(): value, name and type from one input.
To try one of these calls on any page without a task, open the online Selenium compiler for Java.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/TextFields");
// TODO: Find element by ID "lastNameWithPlaceholder"
// TODO: Get its "placeholder" attribute
// TODO: Print in format: "Placeholder: <value>"
driver.quit();
}
}