Top 10 Selenium interview questions

people talking cartoon

Selenium is an open-source automation testing tool that is widely used to test web applications. It has become the go-to automation testing framework for developers and QA testers because of its flexibility and ease of use. As a result, many companies are looking for skilled Selenium professionals. If you’re preparing Selenium interview questions, it’s important to be prepared for tricky questions. In this article, we’ll discuss the top 10 tricky Selenium interview questions and how to answer them.

  1. What are the different types of locators in Selenium?
Answer: The different types of locators in Selenium are ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS Selector, and XPath. These locators are used to identify web elements on a web page.
  1. What is the difference between findElement() and findElements()?
Answer: findElement() is used to find the first web element matching the specified locator on a web page, while findElements() is used to find all the web elements matching the specified locator on a web page. The findElement() method returns a single WebElement object, while the findElements() method returns a list of WebElement objects.
  1. What is the difference between Implicit wait and Explicit wait?
Answer: Implicit wait is a global setting that waits for a certain amount of time before throwing an exception if an element is not found. Explicit wait, on the other hand, waits for a specific condition to be met before proceeding to the next step. Implicit wait is set at the beginning of the test and is applied to all the elements on the page, while explicit wait is set for a specific element or condition.
  1. What is the difference between driver.close() and driver.quit()?
Answer: driver.close() closes the current browser window, while driver.quit() closes all the browser windows and ends the WebDriver session. If there are multiple browser windows open, driver.close() will only close the current window, while driver.quit() will close all the windows and end the session.
  1. How do you handle alerts in Selenium?
Answer: To handle alerts in Selenium, we use the Alert interface provided by WebDriver. We can use the accept() method to accept the alert, the dismiss() method to dismiss the alert, and the getText() method to get the text of the alert.
  1. How do you handle frames in Selenium?
Answer: To handle frames in Selenium, we use the switchTo() method provided by WebDriver. We can switch to a frame by using its index, name, or WebElement. We can switch back to the default content using the defaultContent() method.
  1. How do you handle dropdowns in Selenium?
Answer: To handle dropdowns in Selenium, we use the Select class provided by WebDriver. We can select an option by using its value, index, or visible text. We can also get all the options in a dropdown using the getOptions() method.
  1. How do you handle multiple windows in Selenium?
Answer: Handling multiple windows in Selenium can be tricky but it is an essential part of automated testing. There are different scenarios where a web application may open multiple windows, such as pop-ups, advertisements, or multiple tabs. In Selenium, there are several ways to handle multiple windows:


getWindowHandles() method:
- The getWindowHandles() method returns a set of all the window handles available. You can then switch between the windows using the switchTo() method. The syntax for this method is as follows:

Set<String> handles = driver.getWindowHandles(); Iterator<String> it = handles.iterator(); String mainWindow = it.next(); String childWindow = it.next(); driver.switchTo().window(childWindow);

In this example, we are switching to the second window by retrieving the second window handle from the set.


getWindowHandle() method:
- The getWindowHandle() method returns the handle of the current window. You can use this method to switch back to the main window after performing an action in a child window. The syntax for this method is:

mainWindow = driver.getWindowHandle(); // perform action that opens new window driver.switchTo().window(mainWindow);

In this example, we are switching back to the main window by retrieving the main window handle and using the switchTo() method.


Using window title:
- If you know the title of the window that you want to switch to, you can use it to switch between the windows. The syntax for this method is:

for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); if (driver.getTitle().equals("New Window Title")) { break; } }

In this example, we are iterating through all the windows, switching to each window and checking if the window title matches the expected title.


Using window index:
- If you know the index of the window that you want to switch to, you can use it to switch between the windows. The syntax for this method is:

List<String> windows = new ArrayList<String>(driver.getWindowHandles()); driver.switchTo().window(windows.get(1));

In this example, we are storing all the window handles in a list and switching to the second window by specifying the index.


Handling multiple windows in Selenium requires careful planning and execution. It's important to identify the right window to switch to, especially when dealing with pop-ups and advertisements. The above methods can help you handle multiple windows effectively and improve the reliability and accuracy of your test scripts.
  1. What is Page Object Model (POM)?
Answer: Page Object Model (POM) is a design pattern used in Selenium to make the test scripts more readable, maintainable, and reusable. In POM, each web page is represented as a Java class, and the web elements on the page are defined as instance variables. The actions that can be performed on the web elements are defined as methods.
  1. How do you handle synchronization issues in Selenium?
Answer: Handling synchronization issues in Selenium is crucial to ensure that your test scripts run smoothly and accurately. Synchronization issues occur when the automation script executes faster than the web page being tested, leading to errors such as element not found or stale element reference. To handle synchronization issues in Selenium, there are two types of waits that can be used: Implicit Wait and Explicit Wait.


Implicit Wait:
- An implicit wait instructs the WebDriver to wait for a certain amount of time before throwing a NoSuchElementException if the element is not found on the web page. It is applied globally to all the web elements in the script, and the maximum wait time is set once at the beginning of the script. The syntax for implicit wait is:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

This sets the implicit wait time to 10 seconds.


Explicit Wait:
- An explicit wait instructs the WebDriver to wait for a specific condition to be met before proceeding to the next step. It is used when the implicit wait is not sufficient, or when a more granular control is required. Some of the commonly used conditions for explicit wait are:
 - elementToBeClickable() – Waits for the element to be clickable
 - visibilityOfElementLocated() – Waits for the element to be visible on the web page
 - textToBePresentInElement() – Waits for the specified text to be present in the element

The syntax for explicit wait is:

WebDriverWait(driver, 10); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id")));

This sets an explicit wait for 10 seconds for the element with the id 'element_id' to be visible on the web page.


Using waits effectively in your Selenium script can help you handle synchronization issues and improve the reliability and stability of your test scripts. However, it's important to use waits judiciously and not to rely too heavily on them as they can slow down the test execution time.

With this, we have come to an end of our article on Top 10 Selenium interview questions. Do let us know in the comments if you feel we have missed any frequently asked interview questions on Selenium.


Leave a Reply