Selenium Interview Questions

1. What is Selenium?

Selenium is a collection of tools for automation web based applications. It consists of Selenium IDE, WebDriver and Grid.

2. What are the testing types supported by Selenium?

Selenium supports Functional and Regression Testing.

3. How can we test Windows based application using Selenium?

Selenium doesn’t support windows application testing. It is designed for web based application.

4. What happens when there are some windows or popup appears in browser during test?

If a browser or Java Script alert popup appears, that can be handled using Alert class. For any windows based popup, there is no direct solution from selenium, but there are some workaround using third party tools like Auto It, but with limitations.

5. How to record script in Selenium?

Selenium IDE is a record and playback tool which is distributed as Firefox Plugin. Selenium Web Driver doesn’t have record and playback feature.

6. How to perform remote execution in Selenium?

Remote execution is done using selenium grid by configuring nodes.

7. What are the different types of locators in Selenium?

The different types of locators are as follows :
 1. ID
 2. Classname
 3. Name
 4. Tagname
 5. LinkText
 6. PartialLinkText
 7. XPath
 8. CSS Selector
 9. DOM

8. Which locator is the fastest and and what are your recommendations?

Note:- There are lot of debate on this, but your answer should be
It depends on the architecture of the application, but from the perspective of DOM, CSS Selector is the fastest as it is already loaded in the browser. The ideal recommendation will be ID as if there is unique id for every element. There are some cases where ID will be dynamic, in such cases, CSS Selector and XPath is the best option.

9. What is XPath and what are the different types of XPath?

XPath is used to locate a element based on its XML path. XML stands for Extensible Markup XPath can be used to locate HTML elements.

The XPath is used in html to traverse between various elements across the entire page by using the reference path given by the user.

XPaths can be categorized as absolute and relative XPaths

Absolute Path : - A single slash ‘/’ is used to denote the absolute path ie. it is created to start the selection from the root node of the document.

Relative Path :- A double slash ‘//’ is used to denote the relative path ie. it is created to start the selection from anywhere within the matching node of the document.

10. Which XPath you would recommend?

Relative XPath. Absolute XPath may fail if application structure changes.

11. What is webdirver in selenium?

Note:- This is little tricky

Its WebDriver Server, is an API that communicates with the browser and the client implementation scripts/code.

WebDriver (Code) is an interface.

12. What is the difference between getwindowhandle and getwindowhandles?

getwindowhandle : It is used to get the address of the current browser where the control is active, and it will return a string data type.
getwindowhandles: It is used to get the address of all open browsers, and it will return Set<String> data type.

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

The driver.close() method is used to close the current browser window or tab being controlled by WebDriver.

The driver.quit() method is used to quit the WebDriver and close all associated browser windows or tabs. It also releases the memory and resources occupied by WebDriver.

14. How do you handle alerts and pop-ups in Selenium?

To handle alerts, you can use the Alert class provided by Selenium WebDriver. You can use methods like accept(), dismiss(), and getText() to interact with alerts.

To handle html pop-ups you can use findElement() method to identify the html element and perform required actions on it.

15. What are implicit waits and explicit waits in Selenium?

Implicit waits are used to specify a default waiting time for WebDriver to wait for web elements to appear or become available. It is applied globally and waits for a certain amount of time before throwing an exception if the element is not found.

Explicit waits are used to specify a customized waiting condition for a specific element or a group of elements. You can define the expected condition and the maximum waiting time using explicit wait methods like WebDriverWait and ExpectedConditions.

16. How do you handle frames in Selenium?

To handle frames in Selenium, you can use the switchTo() method of WebDriver along with the frame() method. You can switch to a frame by providing its index, name or ID, or by passing the web element representing the frame.

17. How do you handle dropdowns in Selenium?

To handle dropdowns, you can use the Select class provided by Selenium WebDriver. You can create a Select object by locating the dropdown element and then use methods like selectByVisibleText(), selectByValue(), or selectByIndex() to select an option from the dropdown.

18. What are the different types of waits available in Selenium?

Selenium provides three types of waits:

Implicit Wait: It sets a default waiting time for the WebDriver to search for elements. If an element is not immediately available, WebDriver waits for the specified time before throwing an exception.

Explicit Wait: It allows you to define a specific condition and maximum waiting time for a particular element to be present, visible, or clickable.

Fluent Wait: It is a more advanced form of explicit wait that provides flexibility in defining custom conditions and polling intervals.

19. How do you use explicit wait in Selenium WebDriver?

To use explicit wait in Selenium WebDriver, you need to follow these steps:

Create an instance of WebDriverWait class, passing the WebDriver instance and the maximum waiting time as parameters.

Use the until method of WebDriverWait along with the ExpectedConditions class to define the specific condition you want to wait for.

Apply the explicit wait condition to the desired element using the appropriate method, such as elementToBeClickable, visibilityOfElementLocated, etc.

20. What is the difference between implicit wait and Thread.sleep()?

Implicit Wait: It is a dynamic wait mechanism provided by Selenium WebDriver. It waits for a certain period for elements to appear or become available before throwing an exception. Implicit wait is more efficient as it does not always wait for the full specified time.

Thread.sleep(): It is a static wait method provided by Java. It pauses the execution of the script for the specified time, regardless of whether the element is available or not. Thread.sleep() can introduce unnecessary delays in test execution and is generally discouraged for synchronization purposes in test automation.

21. What are the different exceptions in selenium?

1. NoSuchElementException:
This exception is thrown when an element cannot be found on the web page using the given locator. It usually occurs when the locator used to identify the element is incorrect or when the element is not present in the DOM.

2. TimeoutException:
TimeoutException is thrown when a certain operation or action does not complete within the specified time limit. For example, if an element is not visible or clickable within the specified timeout, a TimeoutException is raised.

3. ElementNotVisibleException:
This exception is thrown when an element is present in the DOM, but it is not visible on the web page. It usually occurs when an element is hidden or covered by another element or when it is not within the visible viewport.

4. ElementNotInteractableException:
ElementNotInteractableException is thrown when an element is present in the DOM but cannot be interacted with, such as clicking, typing, or selecting options. It may occur if the element is disabled, read-only, or not in a state to accept user input.

5. StaleElementReferenceException:
This exception occurs when a previously located element is no longer attached to the DOM or has become stale. It can happen when the web page gets refreshed, modified, or navigated away, making the previously found element no longer valid.

6. WebDriverException:
WebDriverException is a generic exception that represents various WebDriver-related errors. It can occur due to issues like communication errors with the browser driver, problems with the WebDriver setup, or unexpected errors during the execution of WebDriver commands.