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.