IT Career Guide
Search

Selenium Interview Questions: 30 Answers with Examples

Two testers reviewing automated test results on a computer

Selenium interview questions focus on WebDriver basics, locators, waits, handling windows, frames and alerts, framework design such as the Page Object Model, and how you deal with flaky tests. Selenium remains one of the most asked tools in automation testing interviews in India, for freshers and experienced testers alike. This guide covers 30 Selenium interview questions with short, accurate answers and Java examples that match Selenium 4.

Key takeaways

  • Selenium 4 uses the W3C WebDriver standard, and Selenium Manager can download matching browser drivers automatically.
  • Prefer stable locators such as id, name and well-written CSS selectors over long absolute XPath.
  • Use explicit waits with WebDriverWait and ExpectedConditions instead of Thread.sleep to reduce flaky tests.
  • findElement throws NoSuchElementException when nothing matches, while findElements returns an empty list.
  • The Page Object Model keeps locators and page actions in separate classes, which makes tests easier to maintain.

Basic Selenium interview questions

1. What is Selenium?

Selenium is an open-source suite for automating web browsers. It is mainly used for functional and regression testing of web applications and supports Java, Python, C#, JavaScript and Ruby.

2. What are the components of Selenium?

  • Selenium WebDriver: the API that drives browsers through code.
  • Selenium IDE: a browser extension for recording and playing back tests.
  • Selenium Grid: runs tests in parallel across machines and browsers.

3. What is new in Selenium 4?

Selenium 4 follows the W3C WebDriver standard, adds relative locators, improved window and tab handling with newWindow, a redesigned Grid, Chrome DevTools access for Chromium browsers and Selenium Manager for automatic driver management.

4. What are the limitations of Selenium?

It automates only web browsers, not desktop apps. It cannot read CAPTCHAs or handle OTPs without workarounds, has no built-in reporting, and needs a framework such as TestNG or JUnit for assertions and test management.

5. How do you launch a browser in Selenium 4?

WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com");

With recent Selenium 4 versions you usually do not need to set the driver path manually, because Selenium Manager finds or downloads the right driver.

6. What is the difference between driver.close() and driver.quit()?

close() closes the current window. quit() closes all windows and ends the WebDriver session. Always call quit() in your teardown.

7. What is the difference between get() and navigate().to()?

Both open a URL. navigate() also gives back(), forward() and refresh(), so it supports browser history actions.

Locator questions

8. Which locators does Selenium support?

id, name, className, tagName, linkText, partialLinkText, cssSelector and xpath. Selenium 4 adds relative locators such as above, below, toLeftOf, toRightOf and near.

9. Which locator should you prefer?

Use a unique, stable id or name first, then a clear CSS selector, and XPath when you need text matching or moving up the DOM. Agree with developers on test attributes like data-testid where possible.

10. What is the difference between absolute and relative XPath?

Absolute XPath starts from the root with a single slash, for example /html/body/div[2]/form/input, and breaks with any layout change. Relative XPath starts with a double slash, such as //input[@id='email'], and is much more stable.

11. Give useful XPath functions.

//button[text()='Login']
//a[contains(@href,'courses')]
//input[starts-with(@id,'user')]
//label[text()='Email']/following-sibling::input

12. What is the difference between findElement and findElements?

findElement returns the first matching WebElement and throws NoSuchElementException if none is found. findElements returns a list, which is empty if nothing matches, so it is handy for checking presence.

Wait and synchronisation questions

13. What types of waits does Selenium have?

  • Implicit wait: a global time to keep polling when finding elements.
  • Explicit wait: waits for a specific condition on a specific element.
  • Fluent wait: an explicit wait with custom polling interval and ignored exceptions.

14. Show an explicit wait in Selenium 4.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(
    ExpectedConditions.elementToBeClickable(By.id("submit")));
button.click();

15. Why should you avoid Thread.sleep?

It always waits the full time, slowing tests, and still fails if the page takes longer. Condition-based waits finish as soon as the condition is met.

16. Should you mix implicit and explicit waits?

The Selenium documentation advises against mixing them, because it can cause unpredictable wait times. Most teams use explicit waits only.

Handling browser elements

17. How do you handle a dropdown?

Select city = new Select(driver.findElement(By.id("city")));
city.selectByVisibleText("Pune");

The Select class works only with HTML select tags. Custom dropdowns are handled by clicking and choosing elements normally.

18. How do you handle alerts?

Alert alert = driver.switchTo().alert();
String text = alert.getText();
alert.accept();   // or alert.dismiss()

19. How do you handle frames?

Switch into the frame using driver.switchTo().frame() with an index, name, id or WebElement, do your actions, then return with driver.switchTo().defaultContent().

20. How do you switch between windows or tabs?

String parent = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(parent)) {
        driver.switchTo().window(handle);
    }
}

21. How do you perform mouse hover, drag and drop or right-click?

Use the Actions class, for example new Actions(driver).moveToElement(menu).perform() for hover, dragAndDrop(source, target) and contextClick(element).

22. When do you use JavascriptExecutor?

When normal WebDriver actions cannot reach an element, such as scrolling to a specific position or reading values set by scripts. Use it sparingly, because it bypasses real user behaviour.

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

23. How do you take a screenshot?

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

Selenium 4 also lets you capture a screenshot of a single WebElement.

24. How do you upload a file?

If the page uses an input of type file, call sendKeys with the absolute file path on that input. No need to interact with the operating system dialog.

Exceptions and framework questions

25. What is StaleElementReferenceException?

It occurs when an element you found earlier is no longer attached to the DOM, often after a page refresh or re-render. Find the element again, or wait for the new element before acting.

26. Name other common Selenium exceptions.

NoSuchElementException, TimeoutException, ElementNotInteractableException, ElementClickInterceptedException, NoSuchFrameException and NoAlertPresentException.

27. What is the Page Object Model?

A design pattern where each page has a class holding its locators and methods, such as LoginPage.login(user, pass). Tests call these methods instead of using locators directly, so a UI change needs updating in one place only.

28. Which TestNG annotations are commonly used?

@BeforeSuite, @BeforeClass, @BeforeMethod, @Test, @AfterMethod, @AfterClass and @DataProvider for data-driven tests. TestNG also supports groups, priorities and parallel runs through testng.xml.

29. What is Selenium Grid used for?

Grid runs tests on remote machines and different browser versions in parallel. You create a RemoteWebDriver pointing to the Grid URL with the desired browser options.

30. How do you reduce flaky Selenium tests?

  • Use explicit waits and stable locators.
  • Keep tests independent, with their own test data.
  • Avoid depending on test order.
  • Reset state through APIs or the database instead of long UI steps.
  • Capture screenshots and logs on failure to find the real cause.

How to prepare for a Selenium interview

Build a small framework yourself: Java or Python, TestNG or pytest, Page Object Model, explicit waits, a config file and a simple report. Push it to GitHub and be ready to explain every design choice. If you are starting out, follow our Selenium tutorial for beginners, revise testing fundamentals with manual testing interview questions, and compare a newer tool through Playwright interview questions.

Frequently asked questions

Is Selenium still in demand?

Yes. Many companies have large Selenium suites and still hire people to maintain and extend them, even as newer tools like Playwright and Cypress grow.

Which language is best for Selenium?

Java is the most common in Indian job descriptions, followed by Python. Choose the language your target companies use and learn it well.

Can Selenium test mobile apps?

Not directly. Appium, which uses the WebDriver protocol, is used for mobile app automation.

Can Selenium handle CAPTCHA?

No, and it should not try. In test environments, ask developers to disable CAPTCHA or provide a test bypass.

Explore more guides in our testing category.

Other articles

Radium4 — IT training blog

Practical, plain-English guides for DevOps, cloud, data, Python, testing and security learners.