If the element is missing, findElement throws NoSuchElementException.
Step 2: Analyze try-catch block
The exception is caught, so the catch block prints "Button not found".
Final Answer:
Button not found -> Option B
Quick Check:
Missing element triggers catch block [OK]
Hint: Missing element triggers NoSuchElementException catch [OK]
Common Mistakes:
Assuming findElement returns null if not found
Ignoring exception handling
Confusing findElement with findElements
4. You wrote this selector: 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?
medium
A. Use Thread.sleep() before finding the element.
B. Change the selector to By.tagName("div") to select all divs.
C. Use a more specific attribute like an id or unique data-test attribute.
D. Remove the class condition and select by div only.
Solution
Step 1: Identify fragility cause
The selector matches multiple elements because class btn is not unique.
Step 2: Improve selector specificity
Using a unique attribute like id or data-test makes the selector stable and precise.
Final Answer:
Use a more specific attribute like an id or unique data-test attribute. -> Option C
Quick Check:
Specific unique attributes prevent fragile selectors [OK]
Hint: Pick unique attributes to avoid multiple matches [OK]
Common Mistakes:
Selecting all divs causes more fragility
Ignoring selector uniqueness
Using waits instead of fixing selectors
5. You have a test that uses this selector: 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?
hard
A. Ask developers to add a unique data-test attribute and use it in selectors.
B. Use XPath with contains() to match partial class names.
C. Use absolute XPath from the root element to the button.
D. Avoid using selectors and rely on screen coordinates.
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
Unique data-test attributes are designed for testing and rarely change, making tests stable.
Final Answer:
Ask developers to add a unique data-test attribute and use it in selectors. -> Option A
Quick Check:
Stable test attributes prevent fragile selectors [OK]
Hint: Use dedicated test attributes like data-test for stable selectors [OK]
Common Mistakes:
Using fragile class names in selectors
Using absolute XPath which breaks easily
Relying on screen coordinates which are unreliable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.