Quick answer: These 42 Selenium interview questions and answers cover what is asked most in automation tester, QA automation engineer and SDET interviews: Selenium basics, locators and XPath, waits, handling web elements, frameworks and TestNG, and Grid, CI/CD and best practices. Key answers include short code examples.
Read each answer, then close the page and explain it aloud in two or three sentences. That is exactly what interviewers check.
Contents
- Selenium Basics
- Locators
- Waits and Synchronisation
- Handling Web Elements
- Frameworks and TestNG
- Grid, CI/CD and Best Practices
- How to prepare
- FAQs
1. Selenium Basics Interview Questions
Q1. What is Selenium?
Selenium is an open-source suite of tools for automating web browsers. It is mainly used to automate functional and regression testing of web applications, and supports Java, Python, C#, JavaScript and other languages on all major browsers.
Q2. What are the components of Selenium?
Selenium WebDriver (the main API that drives browsers), Selenium IDE (a record-and-playback browser extension) and Selenium Grid (runs tests in parallel across machines and browsers). Selenium RC is the old, retired version.
Q3. What are the advantages and limitations of Selenium?
Advantages: free and open source, many languages and browsers, large community, and integration with TestNG, JUnit, Maven, Jenkins and cloud grids. Limitations: web applications only, no built-in reporting, cannot automate CAPTCHAs or desktop apps, and needs programming skills.
Q4. What is Selenium WebDriver?
WebDriver is an interface that controls a real browser through its browser driver, such as ChromeDriver or GeckoDriver, using the W3C WebDriver protocol. Each command, such as click or sendKeys, is sent to the driver, which performs it in the browser.
Q5. What is Selenium Manager?
Since Selenium 4.6, Selenium Manager automatically downloads the right browser driver, so you no longer need to download ChromeDriver manually or use WebDriverManager in most cases.
Q6. Write a basic Selenium script in Java.
Create a driver, open a URL, interact with elements and close the browser.
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/login");
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("secret");
driver.findElement(By.cssSelector("button[type='submit']")).click();
System.out.println(driver.getTitle());
driver.quit();Q7. What is the difference between driver.close() and driver.quit()?
close() closes only the current browser window. quit() closes all windows and ends the WebDriver session, so it should be used at the end of a test.
Q8. What is the difference between driver.get() and driver.navigate().to()?
Both open a URL and wait for the page to load. navigate() also offers back(), forward() and refresh(), and keeps the browser history.
2. Locators Interview Questions
Q9. What locators does Selenium support?
id, name, className, tagName, linkText, partialLinkText, cssSelector and xpath. Selenium 4 also adds relative locators such as above, below, toLeftOf, toRightOf and near.
Q10. Which locator is the best?
id is usually the fastest and most stable when it is unique. CSS selectors are fast and readable. Use XPath when you need to navigate by text or to parent and sibling elements. Avoid long absolute paths.
Q11. What is the difference between absolute and relative XPath?
Absolute XPath starts from the root with a single slash, such as /html/body/div[2]/form/input, and breaks when the layout changes. Relative XPath starts with // and searches anywhere, such as //input[@id='email'], which is more stable.
Q12. Give some useful XPath functions.
contains(), starts-with(), text(), and axes such as parent, following-sibling and ancestor.
//button[text()='Login']
//input[contains(@id,'user')]
//a[starts-with(@href,'/products')]
//label[text()='Email']/following-sibling::inputQ13. 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 is found.
Q14. How do you handle dynamic elements whose IDs change?
Use stable attributes, partial matches with contains() or starts-with(), text-based XPath, parent–child relationships, or ask developers to add test IDs such as data-testid.
3. Waits and Synchronisation Interview Questions
Q15. Why are waits needed in Selenium?
Web pages load content asynchronously, so an element may not be present or clickable immediately. Without waits, scripts fail with NoSuchElementException or ElementNotInteractableException.
Q16. What is the difference between implicit, explicit and fluent waits?
An implicit wait sets a global time for finding elements. An explicit wait (WebDriverWait) waits for a specific condition on a specific element. A fluent wait is an explicit wait where you also set the polling interval and exceptions to ignore. Do not mix implicit and explicit waits.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
btn.click();Q17. Why should you avoid Thread.sleep()?
Thread.sleep() always waits the full time, slowing tests down, and still fails if the page takes longer. Explicit waits continue as soon as the condition is met.
Q18. What are common ExpectedConditions?
visibilityOfElementLocated, elementToBeClickable, presenceOfElementLocated, textToBePresentInElement, alertIsPresent, frameToBeAvailableAndSwitchToIt and invisibilityOfElementLocated.
4. Handling Web Elements Interview Questions
Q19. How do you handle dropdowns?
For HTML select elements, use the Select class with selectByVisibleText, selectByValue or selectByIndex. For custom dropdowns built with divs, click to open and then click the option.
Select country = new Select(driver.findElement(By.id("country")));
country.selectByVisibleText("India");Q20. How do you handle alerts?
Switch to the alert and accept, dismiss, read or type into it.
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();Q21. How do you handle frames?
Switch into the frame by index, name or WebElement, interact, and switch back with defaultContent().
Q22. How do you handle multiple windows or tabs?
Store the current window handle, get all handles, switch to the new one, and switch back when done.
String main = driver.getWindowHandle();
for (String h : driver.getWindowHandles())
if (!h.equals(main)) driver.switchTo().window(h);
// ... work in new window
driver.switchTo().window(main);Q23. How do you perform mouse hover, drag-and-drop and right-click?
Use the Actions class: moveToElement for hover, dragAndDrop for drag and drop, contextClick for right-click and doubleClick for double-click, then call perform().
Q24. How do you upload a file?
If the page uses an input of type file, send the file path with sendKeys. Otherwise, OS dialogs need other tools, because Selenium cannot control them.
Q25. How do you take a screenshot?
Cast the driver to TakesScreenshot and call getScreenshotAs. Selenium 4 can also capture a single element.
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Files.copy(src.toPath(), Path.of("failure.png"));Q26. When would you use JavaScriptExecutor?
When normal WebDriver actions do not work, for example to scroll to an element, click a hidden element, or read values set by scripts. It should be a last resort, because it bypasses real user behaviour.
Q27. How do you handle web tables?
Locate the table rows and cells with XPath or CSS, loop through them, and read or compare the text, for example to find the row containing a particular order number.
5. Frameworks and TestNG Interview Questions
Q28. What is a test automation framework?
A framework is a set of guidelines, structure and reusable code for writing and maintaining automated tests, such as data-driven, keyword-driven, hybrid and BDD frameworks.
Q29. What is the Page Object Model?
In POM, each page of the application has a class that stores its locators and actions. Tests call these methods instead of using locators directly, so when the UI changes you update only one class.
public class LoginPage {
private final WebDriver driver;
private final By user = By.id("username");
private final By pass = By.id("password");
private final By submit = By.cssSelector("button[type='submit']");
public LoginPage(WebDriver driver) { this.driver = driver; }
public void login(String u, String p) {
driver.findElement(user).sendKeys(u);
driver.findElement(pass).sendKeys(p);
driver.findElement(submit).click();
}
}Q30. What is Page Factory?
Page Factory initialises page object elements declared with @FindBy annotations using PageFactory.initElements. Many teams now prefer plain By locators in POM for simplicity.
Q31. What is TestNG and why is it used?
TestNG is a testing framework for Java that adds annotations, test grouping, priorities, dependencies, parameterisation, data providers, parallel execution and HTML reports.
Q32. What is the order of TestNG annotations?
@BeforeSuite, @BeforeTest, @BeforeClass, @BeforeMethod, @Test, @AfterMethod, @AfterClass, @AfterTest and @AfterSuite.
Q33. What is a DataProvider?
A DataProvider supplies multiple sets of data to a test method, so the same test runs once for each set, which is the basis of data-driven testing.
Q34. What is the difference between hard and soft assertions?
A hard assertion (Assert) stops the test immediately when it fails. A soft assertion (SoftAssert) records failures and continues, reporting all of them when assertAll() is called.
Q35. What is BDD with Cucumber?
Behaviour-Driven Development writes tests as plain-English scenarios in Gherkin (Given, When, Then) in feature files, and step definitions connect them to Selenium code. It helps business and technical teams share understanding.
6. Grid, CI/CD and Best Practices Interview Questions
Q36. What is Selenium Grid?
Selenium Grid runs tests on multiple machines, browsers and operating systems in parallel. Selenium 4 Grid can run as a standalone server or as hub and node components, and works well with Docker.
Q37. How do you run Selenium tests in Jenkins?
Store the project in Git, build it with Maven or Gradle, and create a Jenkins job or pipeline that runs mvn test on a schedule or on every commit, then publish TestNG or Allure reports.
Q38. What is headless browser testing?
Headless mode runs the browser without a visible window, which is faster and suits CI servers. In Chrome you add the –headless=new argument through ChromeOptions.
Q39. What are common Selenium exceptions?
NoSuchElementException, StaleElementReferenceException, TimeoutException, ElementClickInterceptedException, ElementNotInteractableException and NoSuchFrameException.
Q40. What is a StaleElementReferenceException and how do you fix it?
It happens when an element you found earlier is no longer attached to the page, usually after a refresh or re-render. Find the element again just before using it, or wait for the page to stabilise.
Q41. What are the best practices for Selenium automation?
Use the Page Object Model, explicit waits instead of sleeps, stable locators, independent tests, test data outside code, clear reporting with screenshots on failure, and run tests in CI regularly.
Q42. What are alternatives to Selenium?
Playwright and Cypress are popular modern tools for web automation, and Appium is used for mobile apps. Selenium is still the most widely requested in job descriptions.
How to Prepare for a Selenium Interview
- Write locators live: practise XPath and CSS selectors on real websites.
- Explain waits clearly: implicit versus explicit versus fluent, and why Thread.sleep is bad.
- Know your framework: be ready to draw your project structure with POM, TestNG and Maven.
- Handle tricky elements: frames, windows, alerts, dropdowns and stale elements.
- Show CI experience: running tests from Jenkins or GitHub Actions with reports.
Next steps: revise testing basics with manual testing interview questions, strengthen coding with Java interview questions, and see all subjects on our interview questions page.
More interview preparation: see all interview questions, including Python, SQL, SQL query, Java, JavaScript, React, C++, .NET, Spring Boot, Node.js, DSA, OOPs, DBMS, operating system, networking, machine learning, data science, AI, Excel, Power BI, DevOps, manual testing, Selenium, cyber security and HR.
Frequently Asked Questions
Which language should I learn for Selenium?
Java is the most requested in Indian job descriptions, followed by Python. Choose one and learn its basics well before Selenium.
Which Selenium topics are most asked in interviews?
Locators and XPath, waits, handling alerts, frames and windows, the Page Object Model, TestNG annotations and common exceptions.
Do I need manual testing knowledge for Selenium jobs?
Yes. Automation testers must understand test design, the STLC and bug reporting, because they decide what to automate and how.
How should I prepare for a Selenium interview?
Build a small framework with POM, TestNG, Maven and reporting on a demo website, run it from Jenkins or GitHub Actions, and be ready to write locators and waits live.