Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 35 ยท Practice and Review

Selenium Interview Questions

Prepare for Selenium interviews with concise answers on WebDriver architecture, locators, waits, exceptions, POM, Grid, flaky tests and Selenium vs Playwright.

Interviewers rarely want a recited definition. They want to hear that you understand how Selenium works, why tests break and what you do about it. This lesson collects the questions that come up again and again, grouped by topic, with answers short enough to say out loud. After each group you will find a runnable stand-in that lets you see the idea instead of memorising it.

Selenium needs a browser and driver, so it cannot run on this site. Code in plain blocks is read-only, and the TryIt blocks are stand-ins written in JavaScript that model the idea honestly. Details such as defaults vary by version and language binding, so say "in the version I use" when you are unsure in a real interview.

What interviewers listen for

Understanding How and why, not just what.
Experience Real failures and fixes.
Judgement Trade-offs, not slogans.
Clarity Short, structured answers.

A good answer states the idea, gives an example and names a pitfall.

1 / 2

Architecture and basics

What is Selenium, and what are its main components?

Selenium is an open-source suite for automating web browsers. Selenium WebDriver is the part most people mean: an API that drives a real browser. Selenium IDE is a record-and-playback browser extension, and Selenium Grid runs tests on many machines and browsers in parallel. The old Selenium RC was retired when WebDriver took over.

How does WebDriver talk to the browser?

Your script uses a language binding (Python, Java, C#, JavaScript, Ruby). The binding sends commands over HTTP, as JSON, to a driver executable such as chromedriver or geckodriver, following the W3C WebDriver standard. The driver translates them into the browser's own automation interface and returns JSON results. Every browser session has a session id used in each URL.

sequenceDiagram
  participant T as Test script
  participant D as Driver (chromedriver)
  participant B as Browser
  T->>D: POST /session (capabilities)
  D->>B: start browser
  D-->>T: session id
  T->>D: POST /session/id/url
  D->>B: navigate
  B-->>D: done
  D-->>T: 200 OK

Each Selenium call becomes one HTTP request to the driver, which acts inside the browser.

The stand-in below turns a short script into the requests a binding would send. The paths and field names follow the W3C standard; the session id is made up. Notice that By.ID becomes a CSS selector on the wire.

JS Stand-in: from Selenium calls to HTTP requests

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What changed in Selenium 4?

Full W3C compliance (no more translation layer), relative locators (above, below, near), a simple API to open a new tab or window, element and full-page screenshots, access to Chrome DevTools Protocol commands, the first steps of the BiDi (bi-directional) protocol, a redesigned Grid and Selenium Manager, which downloads matching drivers automatically. See What Is New in Selenium 4.

What is the difference between close() and quit()?

close() closes the current window or tab only. quit() ends the whole session, closes all windows and stops the driver process. Always call quit() in teardown, or browsers and drivers pile up in the background.

Locators

Which locators does Selenium support?

ID, name, class name, tag name, link text, partial link text, CSS selector and XPath. Internally the standard has five strategies (css selector, xpath, tag name, link text, partial link text); the others are convenience wrappers.

Which one would you choose, and why?

A dedicated test id attribute first, then a stable id, then a short CSS selector. XPath is for cases CSS cannot express, such as matching on text or walking up to a parent. Avoid long chains that mirror the page structure, because any layout change breaks them. See Choosing Stable Locators.

CSS or XPath?

CSS is usually shorter, faster to read and well supported. XPath can go up the tree (.., ancestor::) and match on text, which CSS cannot. Both work; prefer CSS unless you need those XPath abilities.

What is the difference between find_element and find_elements?

find_element returns the first match or raises NoSuchElementException. find_elements returns a list, which is empty when nothing matches and never raises for that reason. That makes find_elements handy for "is it absent?" checks.

What are relative locators?

A Selenium 4 feature that finds an element by its position relative to another, for example "the input below the label with text Email". They can help with awkward pages, but a real attribute is still more stable.

Waits and exceptions

Explain implicit, explicit and fluent waits.

An implicit wait is a global setting: every find_element retries for up to N seconds before failing. An explicit wait (WebDriverWait with an expected condition) waits for a specific condition on a specific element. A fluent wait is an explicit wait where you also choose the polling interval and which exceptions to ignore. See Implicit, Explicit and Fluent Waits.

Why not mix implicit and explicit waits?

The two can interact in ways that make timeouts unpredictable, for example longer than either value. Prefer explicit waits only, and leave the implicit wait at zero.

Why is sleep() a bad wait?

It always costs its full duration, even when the page is ready after 100 ms, and it still fails when the page is slower than the guess. Conditions adapt to the actual speed of the page.

Name common exceptions and their causes.

  • NoSuchElementException: the locator matched nothing (yet).
  • StaleElementReferenceException: the element was removed or re-rendered after you found it. Find it again.
  • ElementClickInterceptedException: another element, such as an overlay, would receive the click.
  • ElementNotInteractableException: the element exists but is hidden or disabled.
  • TimeoutException: an explicit wait ran out.
  • NoSuchFrameException, NoAlertPresentException, NoSuchWindowException: you switched to something that is not there.
  • SessionNotCreatedException: the browser and driver versions do not match, or the options are invalid.

Interactions

What is the difference between text, get_attribute("textContent") and get_property?

text returns the rendered, visible text. textContent returns the raw text of the node, including hidden text. In Selenium 4, get_attribute reads the attribute or property that best matches the name, while get_property explicitly reads a DOM property such as value or checked.

How do you handle alerts, frames and new windows?

driver.switch_to.alert for JavaScript dialogs (accept, dismiss, read text, send text); driver.switch_to.frame(...) to enter an iframe and default_content() to leave it; and driver.window_handles plus switch_to.window(handle) for new tabs. Selenium does not move to a new tab automatically. See Alerts, Frames and Windows.

How do you select a dropdown option?

For a native <select>, wrap it in Select and use select_by_visible_text, select_by_value or select_by_index. Custom dropdowns made of divs are clicked like any other element.

When would you use JavaScript execution?

As a last resort: reading many values in one call, scrolling or working around a hidden control. A user cannot click through a JavaScript call, so it can hide real usability bugs.

How do you handle file uploads?

Send the absolute path to the <input type="file"> with send_keys. Do not try to drive the operating system's file dialog.

Test design

What is the Page Object Model and why use it?

A design pattern where each page or component is a class that holds its locators and exposes methods for user actions. Tests call login_page.login_as(...) instead of repeating locators. When the page changes, you edit one class. See The Page Object Model.

How do you keep tests independent?

Each test creates its own data and browser session, and does not depend on the order of other tests. Independent tests can run in parallel and fail for one reason only.

How do you make tests data-driven?

Keep inputs outside the test body (parametrized tests, CSV or JSON files, data providers in TestNG) and loop over them. Credentials come from environment variables or a secrets store, never from the repository.

How do you report and debug failures?

Take a screenshot and save the page source in teardown when a test fails, log the steps, and keep reports as CI artifacts. See Reporting and Logging.

Grid, scale and CI

What is Selenium Grid?

A system that routes WebDriver sessions to machines running browsers. A test connects with RemoteWebDriver and asks for capabilities (browser, version, platform); Grid finds a matching node and starts the session there. Grid 4 can run standalone (one process), as hub and nodes, or fully distributed (router, distributor, session map, session queue, event bus, nodes). See Selenium Grid.

How do you run tests in parallel?

Make tests independent, create a driver per test (never share one across threads), then use pytest-xdist or TestNG and JUnit parallel settings, and a Grid or cloud provider for capacity. See Running in CI.

How do you run Chrome without a window?

Add the --headless=new argument to ChromeOptions. Headless is faster and works on servers, but check the important flows in a normal window now and then. See Headless Mode and Browser Options.

Flaky tests

What is a flaky test and what causes it?

A test that sometimes passes and sometimes fails on the same code. Common causes: missing or wrong waits, shared test data, tests depending on order, animations and overlays, unstable locators, slow or shared environments and third-party content.

How do you fix flakiness?

Find the cause: read logs and screenshots, run the test repeatedly, and isolate it. Replace sleeps with conditions, give each test its own data, mock or stub unreliable dependencies, and fix the application when it is genuinely racy. Retries can limit the damage while you work, but they do not remove the cause. See Flaky Tests and Debugging.

Long tests make flakiness worse, because every step must succeed. The stand-in below computes how fast the odds fall, and what retries really buy:

JS Stand-in: the arithmetic of flaky tests

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Selenium and other tools

Selenium or Playwright (or Cypress)?

Selenium's strengths: the W3C standard, every major language, a huge ecosystem, real Safari through safaridriver, and Grid for scale. Playwright's strengths: automatic waiting for actionability, bundled browsers, tracing and a modern API that makes common tests shorter, with fewer waits to write by hand. Cypress runs inside the browser with a great debugging experience but a different architecture. The honest answer depends on your languages, browsers, existing suites and team. See Cypress and Playwright.

Can Selenium test mobile apps or desktop apps?

Selenium drives web browsers, including mobile browsers. For native mobile apps, Appium reuses the WebDriver protocol. See Cloud Providers and Mobile with Appium.

Is Selenium good for web scraping?

It can read pages that need JavaScript, but it is slow and heavy for that. Use an API or a scraping framework when possible, and always respect terms of service and rate limits. See Security, Ethics and Scraping Limits.

Answering well

  • Give the definition in one sentence, then an example from your own work.
  • Name the trade-off: "I prefer explicit waits because...".
  • Admit what you do not know, then explain how you would find out. That is a skill too.
  • Prepare one story about a flaky test you fixed: the symptom, the cause and the fix.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What happens when you call driver.find_elements() and nothing matches?

    find_elements returns an empty list. Only find_element raises NoSuchElementException when nothing matches.
  2. 2. Which statement about close() and quit() is correct?

    Use quit() in teardown so no browser or driver process is left behind.
  3. 3. Why is a test with 60 steps flakier than one with 10 steps, even if each step is equally reliable?

    If each step passes 99% of the time, 60 steps in a row pass only about 55% of the time.
  4. 4. What is the best summary of why implicit and explicit waits should not be mixed?

    The combined behaviour differs between drivers and versions, so total waits may be longer than either setting.
  5. 5. Which locator would you try first on a page you control?

    A test id exists only for tests and does not change with the layout.

Summary

  • WebDriver sends W3C commands as HTTP and JSON from your script to a driver, which controls the browser inside a session.
  • Choose locators by stability: test id, id, short CSS, XPath only when needed.
  • Prefer explicit waits, never sleep(), and know the common exceptions and what each means.
  • Page objects, independent tests and per-test drivers make suites maintainable and parallel-safe.
  • Retries hide flakiness; find the cause. Long tests multiply small failure chances.
  • Compare Selenium, Playwright and Cypress on languages, browsers and team needs, not on slogans.

Finish with the one-page recap in the Selenium Cheat Sheet.