Selenium is an open-source framework used for automating web browsers. It was first created in 2004 by Jason Huggins at ThoughtWorks. It is a powerful suite of tools that supports a wide range of browsers and programming languages, making it the most popular choice for web automation testing.
Selenium has a wide range of applications, primarily in the field of Quality Assurance for functional and regression testing of web applications. Selenium is a foundational skill in the IT industry, and there is a constant demand for skilled automation test engineers.
Freshers and experienced testers looking for a job in software testing should have a good understanding of Selenium and its core concepts. In this article, we will outline 38 Selenium interview questions and answers commonly asked in job interviews. These questions cover concepts like WebDriver, locators, waits, and the Page Object Model. Let’s get started:
Selenium Interview Questions for Automation Testers
Let’s look into some of the basic questions asked in a Selenium interview from testers:
1. What is Selenium?
Selenium is a free, open-source suite of tools for automating web browsers. It is primarily used for testing web applications across different browsers and platforms. Its famous motto is that it “automates browsers. That’s it!”
2. What are the different components of the Selenium Suite?
The Selenium Suite is composed of four main components:
- Selenium IDE (Integrated Development Environment): A browser extension for recording and playing back tests.
- Selenium RC (Remote Control): The first version that allowed writing tests in various programming languages. It is now deprecated.
- Selenium WebDriver: The core component that provides a programming interface to control browsers directly.
- Selenium Grid: A tool to run tests in parallel on different machines, browsers, and operating systems simultaneously.
3. What is Selenium WebDriver?
Selenium WebDriver is an API and protocol that allows you to control a web browser’s actions. It communicates directly with the browser’s native support for automation, making it faster and more stable than Selenium RC.
4. What are the limitations of Selenium?
Selenium can only automate web applications. It cannot be used to test desktop applications or mobile applications (though Appium, a separate tool, uses the WebDriver protocol for mobile). It also does not have built-in reporting features and requires third-party tools like TestNG or JUnit.
5. What are locators in Selenium?
Locators are mechanisms used to identify and find web elements on a page. Selenium WebDriver supports several types of locators:
- ID
- Name
- ClassName
- TagName
- LinkText
- PartialLinkText
- CSS Selector
- XPath
6. What is the difference between XPath and CSS Selector?
Both are used to locate elements. CSS Selectors are generally faster and have a more readable syntax. However, XPath is more powerful as it can traverse both forwards and backward in the DOM, for instance, to find a parent element.
7. What is the difference between /
(single slash) and //
(double slash) in XPath?
/
(single slash) is used to create an absolute XPath, starting from the root node. //
(double slash) is used to create a relative XPath, which finds the element anywhere in the document. //
is used more commonly as it is not affected by changes in the path of the element.
8. What are the different types of waits in Selenium?
Waits are used to handle synchronization issues where a script tries to find an element before it has loaded on the page. The three main types of waits are:
- Implicit Wait
- Explicit Wait
- Fluent Wait
9. What is the difference between an implicit wait and an explicit wait?
An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find an element. It is set once and applies globally. An explicit wait is a code you define to wait for a specific condition to occur before proceeding.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Implicit
WebDriverWait wait = new WebDriverWait(driver, 10); // Explicit
10. What is a Fluent Wait?
A Fluent Wait is a type of explicit wait where you can define the maximum wait time, the polling frequency, and exceptions to ignore. It offers more flexibility than a standard explicit wait.
11. How do you handle dropdown menus in Selenium?
You can handle dropdowns using the Select
class. The Select
class provides methods to select options by their visible text, index, or value attribute.
Select dropdown = new Select(driver.findElement(By.id("my-dropdown")));
dropdown.selectByVisibleText("Option 1");
12. How do you handle alerts in Selenium?
Alerts are JavaScript pop-ups. You can handle them by first switching WebDriver’s context to the alert and then using methods like accept()
, dismiss()
, getText()
, or sendKeys()
.
driver.switchTo().alert().accept();
13. What is the difference between driver.get()
and driver.navigate().to()
?
Functionally, they do the same thing: load a new web page. The navigate()
interface, however, also provides methods to move back (back()
) and forward (forward()
) in the browser’s history.
14. What is the difference between driver.close()
and driver.quit()
?
driver.close()
closes the currently focused browser window. driver.quit()
closes all browser windows opened by the WebDriver session and terminates the WebDriver session itself.
close()
= Closes current window.quit()
= Closes all windows and ends the session.
15. How can you take a screenshot in Selenium?
You can take a screenshot by using the TakesScreenshot
interface and its getScreenshotAs()
method.
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("./screenshot.png"));
16. What is the Actions class in Selenium?
The Actions
class is used to perform complex user interactions like mouse movements, right-clicks, double-clicks, and drag-and-drop operations.
17. How do you handle frames in Selenium?
You must switch WebDriver’s context to the frame before you can interact with elements inside it. You can switch to a frame by its index, name, or ID, or by the web element of the frame itself.
driver.switchTo().frame("my_frame_id");
18. How do you handle multiple windows or tabs?
You can use driver.getWindowHandles()
to get a set of all window handles (unique identifiers). Then, you can iterate through the set and use driver.switchTo().window(handle)
to switch to the desired window.
Further Selenium Interview questions will test your Framework knowledge as well
19. What is the Page Object Model (POM)?
Page Object Model (POM) is a design pattern in which you create an object repository for the web elements on each page. For every page in the application, there is a corresponding Page Class that contains all the web elements and methods to interact with them.
20. What are the advantages of the Page Object Model?
POM makes test automation code more readable, maintainable, and reusable. It separates the test logic from the UI interaction logic, so if the UI changes, you only need to update the code in the Page Class.
21. What is Page Factory?
Page Factory is an inbuilt concept in Selenium’s Page Object Model that is used to initialize the elements of a Page Object. The @FindBy
annotation is used to declare web elements, and they are initialized using the PageFactory.initElements()
method.
@FindBy(id="username")
WebElement usernameField;
22. How can you upload a file using Selenium?
For an <input type="file">
element, you can simply use the sendKeys()
method and pass the absolute path of the file you want to upload.
element.sendKeys("C:\\path\\to\\file.txt");
23. What is an exception in Selenium? Name a few.
An exception is an error that occurs during the execution of a program. Common Selenium exceptions include:
NoSuchElementException
: Thrown when an element cannot be found.TimeoutException
: Thrown when a command does not complete in the specified time.StaleElementReferenceException
: Thrown when an element is no longer attached to the DOM.
24. What is StaleElementReferenceException
?
This exception occurs when the element you are trying to interact with is no longer present on the web page, usually because the page has been refreshed or the DOM structure has changed.
25. How can you scroll down a page using Selenium?
Since WebDriver cannot directly control the scrollbar, you must use the JavascriptExecutor
interface to execute JavaScript code that scrolls the page.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)");
26. What is Selenium Grid?
Selenium Grid is a tool that allows you to run your tests on different machines against different browsers in parallel. It consists of a Hub (a central point that receives test requests) and Nodes (machines where the tests are executed).
27. Can you automate CAPTCHA?
No, you cannot automate CAPTCHA. The purpose of CAPTCHA is to prevent automation and ensure that the user is a human. Automating it would defeat its purpose. In a test environment, this feature is usually disabled or a bypass hook is provided.
28. What is the difference between findElement()
and findElements()
?
findElement()
returns a single WebElement
object (the first one that matches the locator). It throws a NoSuchElementException
if no element is found. findElements()
returns a list of WebElement
objects. It returns an empty list if no elements are found.
29. What are assertions in Selenium testing?
Assertions are used to verify the state of the application. They check if the actual outcome of an action matches the expected outcome. If an assertion fails, the test is marked as failed. Assertions are provided by testing frameworks like TestNG or JUnit.
Assert.assertEquals(actualTitle, expectedTitle);
30. Why do we need TestNG or JUnit with Selenium?
Selenium itself does not have a built-in framework for test management or reporting. TestNG and JUnit are testing frameworks that provide features like test annotations, assertions, parallel execution, and detailed test reports, which are essential for organizing and managing test suites.
31. What are some common TestNG annotations?
Common annotations include @BeforeSuite
, @AfterSuite
, @BeforeTest
, @AfterTest
, @BeforeClass
, @AfterClass
, @BeforeMethod
, @AfterMethod
, and @Test
.
32. How do you run tests in parallel using TestNG?
You can run tests in parallel by setting the parallel
attribute in the testng.xml
file. You can set it to run methods, tests, or classes in parallel.
<suite name="My suite" parallel="methods" thread-count="2">
33. Provide an example of handling an alert.
To handle a simple alert, you switch to it and then accept it. This is typically done after an action that triggers the alert.
driver.findElement(By.id("alertButton")).click();
Alert alert = driver.switchTo().alert();
alert.accept();
34. What is DesiredCapabilities
in Selenium?
DesiredCapabilities
is a class used to set properties of the browser you want to automate, such as browser name, version, and operating system. It is useful for configuring the WebDriver session, especially when running tests on a remote machine or with Selenium Grid.
35. How do you handle cookies in Selenium?
WebDriver provides an interface to interact with cookies. You can add a cookie (driver.manage().addCookie()
), delete a cookie (driver.manage().deleteCookieNamed()
), or get all cookies (driver.manage().getCookies()
).
36. What is a headless browser? How can you run tests in headless mode?
A headless browser is a web browser without a graphical user interface. Running tests in headless mode is faster and useful for CI/CD pipelines. You can enable headless mode by setting browser options.
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
37. Provide an example of using the Actions class.
To perform a mouse-over (hover) action, you can use the Actions
class.
Actions actions = new Actions(driver);
WebElement menu = driver.findElement(By.id("main-menu"));
actions.moveToElement(menu).perform();
38. How do you get the text of a web element?
You can use the getText()
method to retrieve the visible (i.e., not hidden by CSS) text of a web element and its sub-elements.
String pageTitle = driver.findElement(By.tagName("h1")).getText();
Conclusion
Now that you have looked at the top 38 Selenium interview questions and answers, you’re all ready for an automation testing technical job interview round. These Selenium interview questions are surely going to help you ace the interview round. With all the technical knowledge you have of Selenium, these interview questions and answers will help in giving quick and brief answers to any related concepts.
If you want to strengthen your Selenium Skills, you can opt for our Free Selenium Course with Certificate.