By Shivam Singh, founder of TesterRank · Updated September 2026
Do not copy the solutions first. For each element, open DevTools, inspect it, and write down every attribute you see: id, name, data-testid, classes and text. Then pick the most stable one using the priority checklist from earlier in this chapter. This "inspect, list, choose" routine is exactly what you will do every day on a real project.
Open the Banking Portal login page (/banking/login) in your browser. Open DevTools (F12). For each element below, write both a CSS selector and an XPath expression. Test each one in the DevTools Console before writing code.
Write your own locators first. Then check them against these answers; there is often more than one correct answer.
// 1. Page heading
By.cssSelector("h2");
By.xpath("//h2[text()='Sign In to Your Account']");
// 2. User ID input
By.cssSelector("[data-testid='userId']");
By.xpath("//input[@id='userId']");
// 3. Password input
By.cssSelector("[data-testid='password']");
By.xpath("//input[@name='password']");
// 4. Remember checkbox
By.cssSelector("[data-testid='rememberMe']");
By.xpath("//input[@data-testid='rememberMe']");
// 5. Forgot Password link
By.linkText("Forgot Password?");
By.xpath("//a[contains(text(), 'Forgot Password')]");
// 6. Sign In button
By.cssSelector("[data-testid='loginBtn']");
By.xpath("//button[text()='Sign In']");
// 7. OTP button
By.cssSelector("[data-testid='otpLoginBtn']");
By.xpath("//button[contains(text(), 'OTP')]");
// 8. Register link
By.cssSelector("[data-testid='registerLink']");
By.xpath("//a[text()='Register Now']");
// 9. Demo credentials
By.xpath("//strong[text()='rahul@netbank.com']");
By.xpath("//p[contains(text(), 'Demo')]");
// 10. Security footer
By.xpath("//p[contains(text(), 'secure')]");Your locator does not need to match the solution exactly. It is correct if it finds exactly one element, does not depend on position, and would still work if the page design changed. If yours uses a long chain of divs or an index like [3], try again with a stable attribute.
Repeat the exercise on the Shopping and Insurance portals. Each portal uses slightly different naming, which trains you to read unfamiliar HTML quickly.
Key Point: Practice writing both CSS and XPath for the same element. Know when to use which.