Back to QA Automation

Selenium & WebDriver

What is Page Object Model (POM)?
A design pattern where each page of the application is represented by a class. The class contains elements (locators) and methods to interact with them, separating test logic from UI details.
Explain Implicit vs Explicit vs Fluent Wait.
Implicit: Default wait time for all elements. Explicit: Waits for a specific condition (e.g., visibility). Fluent: Explicit wait with polling interval and ignored exceptions.
What are the different Locators in Selenium?
ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS Selector, XPath.
Difference between driver.close() and driver.quit()?
close() closes the current focused window. quit() closes all windows and ends the WebDriver session.
What are the basic CSS selectors for locating elements?
By id (#submit-btn), by class (.btn-primary), by tag (button), and combined for specificity (button.btn-primary, form#login-form). A good test selector is stable (survives layout changes), unique (matches exactly one element), and readable.
Why are data-testid attributes the preferred locator?
data-testid attributes are added specifically for testing, so they don't change when styling or layout changes — unlike CSS classes or generated class names. Recommended priority: data-testid, then aria-label/role, then a stable #id, then semantic attributes like [name], with positional selectors only as a last resort.
How do CSS combinators and pseudo-classes help in test selectors?
Combinators express relationships: descendant (form input), direct child (ul > li), adjacent sibling (label + input). Pseudo-classes target state and position: input:checked, button:not(:disabled), li:first-child, tr:nth-child(2). Prefer state/relationship selectors over absolute positions, which break when the DOM changes.
When should you use XPath instead of CSS selectors?
Use XPath when you need to select by visible text (//button[text()='Login']), navigate to a parent or ancestor (/.., parent::), or express complex logical conditions (and/or/not). CSS is simpler and slightly faster but cannot select by text or walk up the tree. In Selenium/Playwright their performance is effectively equivalent.
How do XPath predicates and positions work?
Predicates filter inside square brackets: //input[@id='email'], //*[@data-testid='login-btn']. Positions are 1-based, not 0-based: //li[1] is the first, //li[last()] the last, //tr[position()>1] skips a header row. Prefer attribute predicates over positional ones for stability.
Which XPath functions are most useful for testing?
text() selects by exact text, contains() does partial matching on text or attributes (//div[contains(@class,'error')]), starts-with() matches attribute prefixes, and normalize-space() ignores surrounding whitespace. Combine with and/or/not for precise, resilient locators.
What are XPath axes and when do you need them?
Axes navigate the DOM tree relative to a node: parent:: (or /..), ancestor::, descendant::, following-sibling::, preceding-sibling::. They are key for context-based location, e.g. find a label by text then its input: //label[text()='Email']/following-sibling::input, or a button in the row containing 'Alice': //tr[td[text()='Alice']]//button.
How do you write robust locators for dynamic elements?
When ids and classes are auto-generated (id='input-7f83b', class='btn-a3f2'), anchor on stable relationships and content instead: locate by visible text or a nearby label and navigate to the target (//label[text()='Username']/following-sibling::input), or scope by a row's content (//tr[.//td[text()='Alice']]//button). aria-label and role attributes are also stable anchors.
What locator anti-patterns should you avoid?
Avoid absolute paths (/html/body/div[2]/form), deep brittle chains (div > div > div > span), generated class names (.sc-bdfxgf, .btn-78sf32), framework-internal classes (.MuiButton-root), and hard positional indexes (li:nth-child(4)) that break when an element is added. Prefer semantic, intention-revealing locators like [data-testid] or [aria-label].