CTCaliberTek
Interview Questions

Selenium WebDriver Fundamentals Interview Questions and Answers

15 Selenium questions on architecture, locators, waits, dropdowns, alerts, frames, windows, and Selenium 4 features.

  1. 1. What is Selenium, and what are its components?

    Selenium is an open-source suite for automating web browsers. Selenium WebDriver provides language bindings (Java, Python, C#, JavaScript, Ruby) that drive real browsers. Selenium Grid runs tests in parallel across machines and browsers. Selenium IDE is a record-and-playback browser extension. Selenium RC is obsolete.

  2. 2. How does WebDriver communicate with the browser?

    Your test code sends commands through the W3C WebDriver protocol over HTTP to a browser-specific driver (ChromeDriver, GeckoDriver, EdgeDriver), which controls the browser and returns the results. Since Selenium 4.6, Selenium Manager automatically finds or downloads the matching driver, so manual driver setup is rarely needed.

  3. 3. What are the different locators in Selenium, and which do you prefer?

    id, name, className, tagName, linkText, partialLinkText, cssSelector, and xpath. Prefer id (fast and unique) or a stable CSS selector, then data-test attributes added for testing. Use XPath when you must move up the DOM or match text, but avoid long absolute paths, which break when the layout changes.

  4. 4. What is the difference between findElement and findElements?

    findElement returns the first matching element and throws NoSuchElementException if none is found. findElements returns a list of all matches, and an empty list if none are found, which makes it handy for checking that something is absent.

  5. 5. What are implicit, explicit, and fluent waits?

    An implicit wait sets a global maximum time for element lookups. An explicit wait (WebDriverWait with ExpectedConditions) waits for a specific condition on a specific element. A fluent wait is an explicit wait where you also set the polling interval and the exceptions to ignore. Prefer explicit waits, and avoid mixing them with implicit waits, which can cause unpredictable delays.

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("save")));
  6. 6. Why is Thread.sleep a bad practice in tests?

    It waits a fixed time whether or not the page is ready, so it slows every run and still fails when the application is slower than the delay. Explicit waits continue as soon as the condition is true, which makes tests faster and more reliable.

  7. 7. What is a StaleElementReferenceException, and how do you handle it?

    It occurs when a reference to an element is no longer valid because the page or the DOM changed (a refresh, or the element was re-rendered). Fix it by locating the element again just before using it, waiting for the new element, and avoiding long-lived stored WebElements. Page objects that find elements on demand reduce the problem.

  8. 8. How do you handle dropdowns?

    For a standard HTML select element, wrap it in the Select class and choose by visible text, value, or index. Custom dropdowns made from divs are handled like any other elements: click to open, then click an option.

    Select country = new Select(driver.findElement(By.id("country")));
    country.selectByVisibleText("India");
  9. 9. How do you handle alerts, frames, and multiple windows?

    Alerts: driver.switchTo().alert() with accept, dismiss, getText, and sendKeys. Frames: switchTo().frame(by index, name, or element) and switchTo().defaultContent() to return. Windows: collect getWindowHandles(), switch to the new handle, and switch back to the original. Selenium 4 can open a new tab or window with switchTo().newWindow(WindowType.TAB).

  10. 10. What is the Actions class used for?

    It performs complex user interactions such as hover, drag and drop, double click, right click, and keyboard combinations.

    Actions actions = new Actions(driver);
    actions.moveToElement(menu).click(subMenu).perform();
    actions.dragAndDrop(source, target).perform();
  11. 11. When do you use JavascriptExecutor?

    When WebDriver's normal commands cannot do the job, such as scrolling an element into view, reading a value computed by JavaScript, or clicking an element hidden behind an overlay. Use it sparingly: it bypasses what a real user could do, so it can hide genuine usability bugs.

  12. 12. What are the new features in Selenium 4?

    Full W3C WebDriver protocol compliance, relative locators (above, below, toLeftOf, toRightOf, near), Selenium Manager for automatic driver management, easier new window and tab creation, element screenshots, a redesigned Grid with observability, and improved DevTools and WebDriver BiDi support for network interception and console logs.

  13. 13. What are relative locators?

    Locators that find elements by their position relative to another element.

    WebElement password = driver.findElement(with(By.tagName("input")).below(By.id("username")));

    They are convenient but position-based, so prefer stable attributes when available.

  14. 14. What is the difference between driver.close and driver.quit?

    close() closes only the current browser window or tab. quit() ends the whole session, closes all windows, and shuts down the driver process. Always call quit() in teardown so you do not leave browser processes running.

  15. 15. How do you take a screenshot, and when would you?

    Cast the driver to TakesScreenshot and call getScreenshotAs. Capture one automatically when a test fails and attach it to the report, which makes failures much easier to diagnose.

    File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

Interview Tips

  • Say why you pick a particular locator and wait; it shows you think about test stability.
  • Mention Selenium Manager and Selenium 4 features to show your knowledge is current.
  • Relate every answer to flaky tests: most Selenium problems are timing or locator problems.