In Selenium tests, why is it best to use unique IDs as selectors?
Think about what makes a selector reliable and less likely to break.
Unique IDs identify exactly one element and rarely change, so tests using them are less fragile.
Given the following Java Selenium code, what element will be selected?
WebElement element = driver.findElement(By.cssSelector("div.content > ul > li.active"));
String text = element.getText();
System.out.println(text);Look carefully at the CSS selector structure and what it targets.
The selector targets an
- inside a
You want to check that a button is both visible and enabled before clicking it. Which assertion is best?
Both conditions must be true to safely interact with the button.
Using logical AND ensures the button is visible and enabled, so the test won't fail due to invisibility or disabled state.
Consider this XPath selector used in a test:
//div[3]/ul/li[2]/a
Why might this cause fragile tests?
Think about what happens if the page structure changes slightly.
Using exact indexes like [3] or [2] means if elements are added or removed, the selector breaks, making tests fragile.
In a Selenium test automation framework, how does mastering selectors improve test reliability and maintenance?
Consider how selectors affect test stability and ease of updates.
Good selectors make tests less brittle and easier to update when UI changes, improving framework robustness and maintainability.
Practice
id or name in Selenium selectors?Solution
Step 1: Understand selector uniqueness
Unique attributes likeidornameidentify elements clearly without confusion.Step 2: Impact on test stability
Using unique selectors reduces the chance tests break when page layout or styles change.Final Answer:
They make tests more stable and less likely to break when the page changes. -> Option DQuick Check:
Unique selectors = stable tests [OK]
- Choosing selectors based on visible text only
- Using generic tags like
divwithout attributes - Ignoring the importance of uniqueness
id in Selenium Java?Solution
Step 1: Recall Selenium locator syntax
The correct method to find by id isBy.id("value").Step 2: Check each option
driver.findElement(By.id("submitBtn")); usesBy.id("submitBtn"), which is correct syntax. Others use incorrect methods or syntax.Final Answer:
driver.findElement(By.id("submitBtn")); -> Option AQuick Check:
Use By.id("...") for id selectors [OK]
- Using assignment (=) instead of method parameter syntax
- Confusing By.name and By.id
- Using non-existent locator methods like By.tag
loginBtn is not present on the page?try {
WebElement button = driver.findElement(By.id("loginBtn"));
System.out.println("Button found");
} catch (NoSuchElementException e) {
System.out.println("Button not found");
}Solution
Step 1: Understand findElement behavior
If the element is missing,findElementthrowsNoSuchElementException.Step 2: Analyze try-catch block
The exception is caught, so the catch block prints "Button not found".Final Answer:
Button not found -> Option BQuick Check:
Missing element triggers catch block [OK]
- Assuming findElement returns null if not found
- Ignoring exception handling
- Confusing findElement with findElements
driver.findElement(By.xpath("//div[@class='btn']")). Your test breaks when the page adds another div with class btn. How can you fix this fragile selector?Solution
Step 1: Identify fragility cause
The selector matches multiple elements because classbtnis not unique.Step 2: Improve selector specificity
Using a unique attribute likeidordata-testmakes the selector stable and precise.Final Answer:
Use a more specific attribute like anidor uniquedata-testattribute. -> Option CQuick Check:
Specific unique attributes prevent fragile selectors [OK]
- Selecting all divs causes more fragility
- Ignoring selector uniqueness
- Using waits instead of fixing selectors
driver.findElement(By.cssSelector("button.submit-btn"))After a page update, the class name changes to
submit-button. What is the best way to prevent such fragile tests in the future?Solution
Step 1: Understand fragility cause
Class names often change due to styling updates, breaking selectors based on them.Step 2: Use stable attributes for selectors
Uniquedata-testattributes are designed for testing and rarely change, making tests stable.Final Answer:
Ask developers to add a uniquedata-testattribute and use it in selectors. -> Option AQuick Check:
Stable test attributes prevent fragile selectors [OK]
- Using fragile class names in selectors
- Using absolute XPath which breaks easily
- Relying on screen coordinates which are unreliable
Ready to prove it?
Pass the test and this pattern moves unknown
