We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Understand how Selenium finds elements on a web page.
Before Selenium can click a button, type into a field, or read some text, it must first find the right element on the page.
The only way it can do that is through locators. If your locators are weak or unstable:
Understanding locators from day one gives you a strong foundation for every future Selenium test you write.
WebDriver and findElement(...)).Selenium does not see the screen the way humans do. It only sees this DOM structure.
findElement(...) or findElements(...), we are always passing a locator.If the locator:
By Class (Concept Only)In Selenium WebDriver (Java), we use the By class to describe how to find elements, for example:
You don't need the exact syntax now—just know that locators tell Selenium "where" in the DOM to look.
Good locators are:
Bad locators cause:
You cannot perform any action (click, type, read text) until Selenium has successfully located the element.
id or name as your first choice for locators. They are easier to understand and usually more stable than complex patterns.For each potential locator idea, ask:
Think through two failure modes:
Before you move to the next labs, define your personal locator strategy (you can refine it later), for example:
data-*).In this introductory lab, we intentionally do not use code.
For now, you only need to understand these ideas:
findElement(...) and findElements(...).By class (e.g., "find the element by id" or "by name").You will see and write real code using these concepts in the upcoming labs, once the foundation is solid.
If you understand this lab well, you're ready to move on to practical locator strategies and start writing real locators in code in the next labs.