Selenium WebDriver interview questions on locators, waits, page object model, frames and windows, exceptions, Selenium Grid and framework design.
50 questions17 Beginner20 Intermediate13 Advanced
Interview Questions
Showing 15 of 50 questions
Answer
Selenium is an open-source suite for automating browsers. Its components are WebDriver for programmatic browser control, Selenium Grid for distributed and parallel runs, and Selenium IDE for record-and-playback.
WebDriver BiDi adds event-driven features like console and network logs
Interview tip: Mention that WebDriverManager or manual driver paths are no longer required with Selenium Manager.
Selenium is an open-source suite for automating browsers. Its components are WebDriver for programmatic browser control, Selenium Grid for distributed and parallel runs, and Selenium IDE for record-and-playback.
Answer
Client bindings send W3C WebDriver commands over HTTP to a browser driver like chromedriver, which controls the browser natively. Selenium 4 dropped the JSON Wire Protocol, so there is no encoding/decoding layer between client and driver.
Key points
Each command is a REST call such as POST /session/{id}/element
RemoteWebDriver talks to Grid using the same protocol
BiDi uses a WebSocket for two-way communication
Client bindings send W3C WebDriver commands over HTTP to a browser driver like chromedriver, which controls the browser natively. Selenium 4 dropped the JSON Wire Protocol, so there is no encoding/decoding layer between client and driver.
Answer
Selenium supports id, name, className, tagName, linkText, partialLinkText, cssSelector and xpath. I prefer id or a dedicated data-testid via CSS, then CSS selectors, and use XPath only when I need text matching or traversal.
Key points
CSS is usually faster and more readable than XPath
Selenium supports id, name, className, tagName, linkText, partialLinkText, cssSelector and xpath. I prefer id or a dedicated data-testid via CSS, then CSS selectors, and use XPath only when I need text matching or traversal.
Answer
findElement returns the first matching WebElement and throws NoSuchElementException if none exist. findElements returns a List, which is empty when nothing matches, so it is useful for checking absence.
Key points
Both respect the implicit wait if one is set
findElements is handy for counting rows or options
Don't use findElements().size() > 0 as a substitute for a proper wait
findElement returns the first matching WebElement and throws NoSuchElementException if none exist. findElements returns a List, which is empty when nothing matches, so it is useful for checking absence.
Answer
An implicit wait sets a global timeout for finding elements. An explicit wait (WebDriverWait) waits for a specific condition like visibility or clickability, and a fluent wait is a configurable explicit wait with custom polling and ignored exceptions.
Key points
Prefer explicit waits; don't mix them with implicit waits, which causes unpredictable timeouts
Selenium 4 uses Duration, e.g. Duration.ofSeconds(10)
Thread.sleep is a hard wait and makes tests slow and flaky
Interview tip: Interviewers often ask 'why not Thread.sleep?' Answer: it always waits the full time and still fails if the app is slower.
An implicit wait sets a global timeout for finding elements. An explicit wait (WebDriverWait) waits for a specific condition like visibility or clickability, and a fluent wait is a configurable explicit wait with custom polling and ignored exceptions.
Answer
For native select elements I use the Select class with selectByVisibleText, selectByValue or selectByIndex. For alerts I wait for alertIsPresent, switch to the Alert and accept, dismiss or sendKeys.
Key points
Custom (div-based) dropdowns need regular click and locate
UnhandledAlertException occurs if an alert blocks other commands
Check getOptions() to validate dropdown contents
java
Select country = new Select(driver.findElement(By.id("country")));
country.selectByVisibleText("India");
driver.findElement(By.id("delete")).click();
Alert alert = new WebDriverWait(driver, Duration.ofSeconds(5))
.until(ExpectedConditions.alertIsPresent());
alert.accept();
For native select elements I use the Select class with selectByVisibleText, selectByValue or selectByIndex. For alerts I wait for alertIsPresent, switch to the Alert and accept, dismiss or sendKeys.
Answer
Use driver.switchTo().frame() by index, name/id or WebElement, and switchTo().defaultContent() to return. For windows, collect getWindowHandles(), switch to the new handle, and switch back using the stored original handle.
Use frameToBeAvailableAndSwitchToIt for loading iframes
parentFrame() moves up one level in nested frames
java
String original = driver.getWindowHandle();
driver.findElement(By.linkText("Open report")).click();
new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.numberOfWindowsToBe(2));
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(original)) driver.switchTo().window(handle);
}
// ... assertions on new tab
driver.close();
driver.switchTo().window(original);
Use driver.switchTo().frame() by index, name/id or WebElement, and switchTo().defaultContent() to return. For windows, collect getWindowHandles(), switch to the new handle, and switch back using the stored original handle.
Answer
The usual ones are NoSuchElementException, StaleElementReferenceException, ElementClickInterceptedException, ElementNotInteractableException and TimeoutException. Most are fixed with correct locators, explicit waits on the right condition, and re-locating elements after DOM changes.
Key points
Stale: element was re-rendered; find it again after the update
Intercepted: an overlay or spinner covers it; wait for invisibility
NotInteractable: hidden or disabled; wait for visibility/clickability
Interview tip: Don't say you wrap everything in try/catch with retries; fix the synchronization.
The usual ones are NoSuchElementException, StaleElementReferenceException, ElementClickInterceptedException, ElementNotInteractableException and TimeoutException. Most are fixed with correct locators, explicit waits on the right condition, and re-locating elements after DOM changes.
Answer
It means the element reference is no longer attached to the DOM, usually after a re-render. I re-locate the element by its By locator after the change, or wait for stalenessOf the old element and then find the new one.
Key points
Store By locators in page objects rather than WebElements
Wait for the action's outcome, not just the element
Common in SPAs with React/Angular re-rendering lists
It means the element reference is no longer attached to the DOM, usually after a re-render. I re-locate the element by its By locator after the change, or wait for stalenessOf the old element and then find the new one.
Answer
POM gives each page a class that holds its locators and user actions, so tests read like business steps and locator changes are made in one place. PageFactory with @FindBy is still available, but its lazy proxies can cause stale element issues and it is no longer actively recommended by the Selenium team, so many teams use plain By fields instead.
Key points
Methods return the next page object for fluent flows
Keep assertions in tests, not page objects
Combine with explicit waits inside action methods
Component objects for shared headers, modals, tables
java
public class LoginPage {
private final WebDriver driver;
private final WebDriverWait wait;
private final By email = By.id("email");
private final By password = By.id("password");
private final By submit = By.cssSelector("button[type='submit']");
public LoginPage(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
public DashboardPage loginAs(String user, String pass) {
wait.until(ExpectedConditions.visibilityOfElementLocated(email)).sendKeys(user);
driver.findElement(password).sendKeys(pass);
driver.findElement(submit).click();
return new DashboardPage(driver);
}
}
POM gives each page a class that holds its locators and user actions, so tests read like business steps and locator changes are made in one place. PageFactory with @FindBy is still available, but its lazy proxies can cause stale element issues and it is no longer actively recommended by the Selenium team, so many teams use plain By fields instead.
Answer
I use annotations like @BeforeMethod/@AfterMethod for driver lifecycle, groups for smoke and regression, @DataProvider for data-driven tests, parallel execution from testng.xml, and listeners for screenshots and reporting.
Key points
IRetryAnalyzer for controlled retries of known-flaky tests
ITestListener onTestFailure to capture screenshots
dependsOnMethods should be used sparingly
JUnit 5 is an equally valid choice with similar features
java
@DataProvider(name = "logins", parallel = true)
public Object[][] logins() {
return new Object[][] { {"admin", "ok"}, {"locked", "error"} };
}
@Test(dataProvider = "logins", groups = "regression")
public void loginTest(String user, String expected) {
// ...
}
I use annotations like @BeforeMethod/@AfterMethod for driver lifecycle, groups for smoke and regression, @DataProvider for data-driven tests, parallel execution from testng.xml, and listeners for screenshots and reporting.
Answer
I store the driver in a ThreadLocal so each test thread gets its own instance, created in setup and quit and removed in teardown. Static shared drivers cause tests to interfere when run in parallel.
Key points
Always call quit() and ThreadLocal.remove() to avoid leaks
Test data must also be unique per thread
Grid or cloud providers supply browser capacity
java
public final class DriverManager {
private static final ThreadLocal<WebDriver> DRIVER = new ThreadLocal<>();
public static void init() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
DRIVER.set(new ChromeDriver(options));
}
public static WebDriver get() { return DRIVER.get(); }
public static void quit() {
if (DRIVER.get() != null) { DRIVER.get().quit(); DRIVER.remove(); }
}
}
I store the driver in a ThreadLocal so each test thread gets its own instance, created in setup and quit and removed in teardown. Static shared drivers cause tests to interfere when run in parallel.
Answer
I use it for things WebDriver can't do natively, like scrolling to a precise position, reading computed values, or interacting with shadow-root edge cases. I avoid JS clicks as a default because they bypass real user behavior and can hide bugs.
Key points
executeScript and executeAsyncScript
Actions class or scrollIntoView for scrolling
Selenium 4 supports getShadowRoot() natively
Interview tip: If asked 'click not working, so you use JS click?', explain you'd first find out what intercepts the click.
I use it for things WebDriver can't do natively, like scrolling to a precise position, reading computed values, or interacting with shadow-root edge cases. I avoid JS clicks as a default because they bypass real user behavior and can hide bugs.
Answer
Grid 4 has a Router, Distributor, Session Map, Session Queue and Nodes, and can run in standalone, hub-and-node or fully distributed mode. Tests use RemoteWebDriver with browser options pointed at the Grid URL, and the Distributor assigns sessions to nodes with matching capabilities.
Key points
Docker images (selenium/standalone-chrome) are common in CI
Kubernetes with KEDA for autoscaling nodes
Cloud providers expose the same RemoteWebDriver endpoint
java
ChromeOptions options = new ChromeOptions();
options.setCapability("se:recordVideo", true);
WebDriver driver = new RemoteWebDriver(
URI.create("http://grid:4444").toURL(), options);
Grid 4 has a Router, Distributor, Session Map, Session Queue and Nodes, and can run in standalone, hub-and-node or fully distributed mode. Tests use RemoteWebDriver with browser options pointed at the Grid URL, and the Distributor assigns sessions to nodes with matching capabilities.
Answer
I check failure screenshots, logs and video from CI, then compare environment differences: headless mode, window size, browser version, slower machines and test data. Then I reproduce with the same settings locally, often using the same Docker image.
Key points
Set an explicit window size in headless mode (e.g. 1920x1080)
Look for missing explicit waits that only pass on fast machines
Check parallel tests sharing accounts or data
Verify environment URLs and secrets
Interview tip: Mention adding a TestNG listener that captures screenshots and page source on failure so CI failures are diagnosable.
I check failure screenshots, logs and video from CI, then compare environment differences: headless mode, window size, browser version, slower machines and test data. Then I reproduce with the same settings locally, often using the same Docker image.