You completed your Selenium certification training and want to make your resume impressive then this is the article you want to read. In this article, we have a list of Selenium automation testing interview questions your interviewer may ask.
39 Selenium Interview Questions to Show Your Testing Framework Skills.
This guide aims to help you excel in your next Selenium developer interview and spruce up your Selenium automation tester resume. Let’s get started!
1. What is Selenium?
Selenium is a powerful open source tool for testing web applications. It allows developers to write scripts in various programming languages, such as Java, C#, Python, Ruby, and more.
This flexibility makes it easy to identify web elements and perform actions like clicking, filling out forms, or capturing screenshots.
Selenium supports a range of browsers, including Chrome, Firefox, Safari, and Internet Explorer, which means developers can test their applications across different browsers.
It’s a suite of software that caters to different testing needs. The suite includes four main components: Selenium Integrated Development Environment (IDE), Selenium Remote Control (RC), WebDriver, and Selenium Grid.
Selenium’s ability to support multiple programming languages, browsers, and operating systems makes it a popular choice for automating web application testing. Its versatility and efficiency make it a valuable tool for developers and testers alike.
2. What are the main components of Selenium and how do they contribute to web application testing?
Selenium is a powerful tool for automating web application testing. It consists of several components, each with a specific role in making web application testing easier.
- Selenium IDE (Integrated Development Environment): Selenium IDE is a simple and user-friendly Firefox plugin that allows developers to record, edit, and debug tests. It’s perfect for beginners who are just starting with Selenium because it’s easy to use and doesn’t require much coding knowledge.
- Selenium RC (Remote Control): Selenium RC, also known as Selenium 1, is a server that allows users to generate test scripts and control a browser. However, Selenium RC has been deprecated and merged with WebDriver to form Selenium.
- Selenium WebDriver: Selenium WebDriver is the successor to Selenium RC and the main component of Selenium 2. WebDriver provides an interface to execute test cases against most modern web browsers. It’s more powerful and flexible than Selenium RC, making it the preferred choice for most Selenium users.
- Selenium Grid: Selenium Grid is a tool used for running tests on multiple machines against different browsers in parallel. It allows for distributed test execution, which can save time and resources. Selenium Grid can execute tests on both the new WebDriver and the old Remote Control.
3. What are the key differences between Selenium 1 and Selenium 2?
Selenium, a popular tool for automating web browsers, has undergone significant changes over time. Selenium 1, also known as Selenium Remote Control (RC), and Selenium 2, known as Selenium WebDriver, are two distinct versions with different approaches to browser automation. Selenium RC, the initial version, operates as a server and uses JavaScript to automate the browser.
This approach, however, is limited by the same-origin policy, which restricts certain actions. Additionally, the test script must go through the RC server to interact with the browser, leading to slower test execution.
In contrast, Selenium WebDriver, the successor to Selenium RC, offers a more straightforward interface for creating test scripts. Unlike Selenium RC, WebDriver does not rely on JavaScript for automation and directly interacts with the browser, thus overcoming the same-origin policy issue. Furthermore, WebDriver does not require a server to interact with the browser, making test execution faster.
4. What are the key differences between the verify and assert commands in Selenium?
In Selenium, both assert and verify commands are used to validate the outcome of a test case. However, they behave differently when the condition being checked fails.
The assert command checks if a certain condition is true or false. If the condition is true, the program continues executing further steps. If the condition is false, the execution stops, and no further test steps are executed. In other words, if an assert command fails, the test is marked as failed, and test suite execution is aborted.
On the other hand, the verify command checks if a condition is true or false without halting program execution. Regardless of whether the condition is true or false, the test continues executing the remaining test steps. If a verify command fails, the test is marked as failed, but the test suite execution continues with the next test.
5. What types of locators are available in Selenium?
Selenium uses locators to find elements on a web page. These locators are HTML properties that uniquely identify elements on the page. There are eight types of locators in Selenium:
- ID: This is the most reliable way to locate an element, as IDs are supposed to be unique for each element.
- Name: This locator is reliable if the name attribute is defined for the element and the name is unique.
- Class name: This locator finds elements by their class attribute. However, multiple elements can have the same class, making it less reliable.
- Tag name: This locator is used when the tag name of the element is unique.
- Link text: This locator finds links by their exact text.
- Partial link text: This locator finds a link by matching a part of the text.
- CSS selector: This is a more advanced way to locate elements based on their CSS properties.
- XPath: This is the most flexible and complex way to locate elements. It can identify dynamic elements and is used when there is no other way to locate an element.
Each locator type has its own strengths and weaknesses, and choosing the right one depends on the specific requirements of your automation test.
6. How do you deal with pop-ups that run in Windows?
You can’t directly handle pop-ups in Windows with Selenium WebDriver because it’s meant to automate web applications. Some ways to get around these pop-ups are available, though.
For example, AutoIT or the Robot class in Java are third-party tools that are often used. AutoIT is a scripting language computer program that lets you interact with pop-up windows by simulating typing, mouse movements, and window changes. Your Selenium script can call a script you wrote in AutoIT that handles a certain pop-up window.
You can also use Java’s Robot class to deal with pop-up windows. For interacting with these pop-ups, it can make native system input events.
The WebDriver’s built-in features can also be used as an alternative. The browser settings can be changed so that files are downloaded instantly to a certain place if the pop-up is a file download dialogue. This way, the dialogue box won’t show up.
Stay aware that these are only temporary solutions that might not always work. It would depend on the needs of your test case to find the best way to deal with pop-ups that appear in Windows.
7. What is the primary distinction between findElement() and findElements() in Selenium WebDriver?
Selenium WebDriver has methods called findElement() and findElements() that can be used to find things on a web page. That being said, they do different things and return different types of data.
The first thing on the web page that fits the given locator value is found by the findElement() method. A “NoSuchElementException” is thrown if the method can’t find a matched element. A WebElement is what this method returns.
The findElements() method, on the other hand, finds all the items on the page that match the locator value that was given. If it can’t find any elements that match, it doesn’t throw an error but instead gives an empty list. This method gives back a List of WebElements.
To sum up, use findElement() when you know there is only one element that matches or when you need the first one. If you think there will be more than one matched element and you need to work with all of them, use findElements().
8. In Selenium, what is an exception test?
In Selenium, an exception test is meant to pass when the system being tested throws an exception. This is usually done to make sure that the system works right when there is an error or when bad data is given.
You can write an exception test in Selenium by adding a try-catch block to your test method. During the try block, you do the thing that should cause an exception. You check that the exception that was thrown is the one you expected in the catch block.
If you’re testing a login form, for instance, you could write an exception test that tries to log in with a username that doesn’t work. What should happen is that the system should throw an exception. If the right exception is thrown, the test should pass.
If you want to use exception tests, you should be smart about it. They can help you test how to handle errors, but they shouldn’t be used instead of validating data and checking for errors in your test methods.
9. Using Selenium, how do you deal with frames?
In Selenium WebDriver, to handle frames, you have to move the driver’s focus from the main page to the frame and back again. You have to do this because WebDriver can only deal with things that are in its current focus, which is usually the home page.
The `switchTo().frame()` method in Selenium WebDriver lets you move the focus to a frame. You can change to a frame in three ways:
By index: If the page has more than one frame, you can get to them by their index number, which starts at 0 for the first frame.
By name or ID: You can switch to the frame using its “name” or “id” property if it has one.
If the frame is a WebElement, you can switch to it by passing the WebElement to the `switchTo().frame()` method.
You can use the `switchTo().defaultContent()` method to go back to the main page after working with the parts inside the frame. You can use the `switchTo().parentFrame()` way to go back to the parent frame if there are any nested frames.
10. What is the difference between driver.close() and driver.quit()?
Both `driver.close()` and `driver.quit()` close the browser in Selenium WebDriver, but they do so in slightly different ways.
When you call `driver.close()`, the browser window that has the attention will close. When the focus is on one of the browser windows that WebDriver has started, `driver.close()` will not close any other windows.
`driver.quit()`, on the other hand, closes all the browser windows that WebDriver has opened. Every open window will be closed when you call `driver.quit()`, no matter where the focus is. This also stops the session between the test script and the WebDriver safely.
To sum up, use `driver.quit()` to end the WebDriver session and close all windows, and `driver.close()` to close the current browser window.
11. What are some ways to handle warnings in Selenium?
The Alert interface in Selenium lets you deal with alerts. It gives you ways to work with JavaScript alerts, confirms, and prompts.
To deal with an alert, you must first use the `switchTo().alert()` way to move the driver’s attention to it. As soon as the alert is in focus, you can use the following Alert interface methods to deal with it:
`accept()`: This method is used to click the alert’s “OK” button.
You can use `dismiss()` to click on the ‘Cancel’ button in the alert.
The `getText()` method is used to get the alert message.
You can enter text into prompt alerts with this method: `sendKeys(String stringToSend)`.
After you’re done with the alert, you can use the `switchTo() to return the attention to the main page.use the defaultContent() method. Noting that you can’t change anything else on the page while the alert is showing is important. You have to either accept or delete the alert or provide input for prompt alerts.
12. What is JavaScriptExecutor used for?
One part of Selenium WebDriver called JavaScriptExecutor is used to run JavaScript code. There are two ways, `executeScript()` and `executeAsyncScript()`, that let you run JavaScript code right in your Selenium WebDriver.
You can run synchronous JavaScript code with the `executeScript()` method. It stops the program from running until the script is run, and then it gives back the result of running the script.
You can run asynchronous JavaScript code with the `executeAsyncScript()` method. It doesn’t stop the program from running and gives you a future object that you can use to get the script’s result when it’s ready.
When you need to do something that WebDriver doesn’t directly support, JavaScriptExecutor comes in very handy. This can be used to do things like scroll through a web page, change the value of a secret input field, or make the mouse move. It’s a strong tool that can make WebDriver do more, but you should be careful how you use it because it can make your tests more complicated and harder to keep up to date.
How are single slashes (/) and double slashes (//) different in XPath?
Single slashes (/) and double slashes (//) are both used in XPath to move between elements in an XML document, but they do different things.
An exact path in XPath is made up of a single slash (/). If the path begins with a single slash, it means that it goes straight to the part that is needed. The search for the element starts at the root node, which is also called the start node of the text.
A relative route in XPath, on the other hand, is made with a double slash (//). A search can begin anywhere in the text when the path begins with a double slash. Nodes in the document from the current node that match the selection are picked, no matter where they are in the document. Like, `//div` would look for all `div` parts in the document.
In short, in XPath, absolute paths use a single slash (/), and relative paths use a double slash (//).
14. What purpose does the AutoIt tool serve?
AutoIt is a free scripting language that can be used to automate the Windows graphical user interface and for other programming tasks. It automates jobs that can’t be done reliably or at all with other languages by simulating keystrokes, mouse movement, and changing windows and controls.
AutoIt is often used as a workaround for Selenium WebDriver to handle pop-ups and dialogs that run in windows that WebDriver can’t directly deal with. When a file dialog box appears during file uploads, for example, Selenium WebDriver cannot connect with it. If you use AutoIt, you can make a script that opens a file and clicks the “Open” button in the file window.
The Selenium script can call the AutoIt script, which makes it easy for the two to work together. This makes AutoIt a useful tool for Selenium WebDriver situations that involve interacting with dialog boxes or pop-ups that run in windows.
15. Talk about the difference between Selenium’s implicit wait and explicit wait.
In Selenium, you can add waiting time to your test scripts with either implicit wait or explicit wait. However, they do different things and behave in different ways.
Implicit wait is a type of wait that stays in place for the whole WebDriver. After being set, the WebDriver will wait a certain amount of time for elements to show up in the DOM before throwing a “NoSuchElementException.” It is set to 0 by default. The default wait is set only once at the beginning of the WebDriver, but it works for everything in your test script.
On the other hand, explicit wait is a type of wait that is set for a certain situation on a certain element. It tells the WebDriver to wait until a certain event happens (like an element being clickable, visible, etc.) before doing anything else. A “TimeoutException” is thrown if the condition is not met in the time that was given. With explicit wait, you can set different waiting conditions for each part, giving you more freedom.
16. What does WebDriverBackedSelenium mean?
Selenium WebDriver has a class called WebDriverBackedSelenium that lets you use the Selenium RC interface while WebDriver is running in the background. This is especially helpful when switching from Selenium RC to WebDriver because it lets you keep using your old Selenium RC code while also using WebDriver’s features and benefits.
You can make an instance of Selenium that handles WebDriver with WebDriverBackedSelenium. This lets you use Selenium RC and WebDriver commands together. So, instead of having to rewrite all of your test scripts at once, you can slowly switch them from Selenium RC to WebDriver by replacing each Selenium RC command with its WebDriver version.
But it’s important to keep in mind that WebDriverBackedSelenium is part of Selenium-WebDriver’s backward compatibility layer, which isn’t being worked on anymore. Because of this, it is suggested that all new test work be done with WebDriver.
17. Explain the difference between driver.getWindowHandles() and driver.getWindowHandle() in WebDriver is what their names mean.
You can handle the current window with driver.getWindowHandle(), which gives a string that holds the handle to the current window.
You can handle more than one window with driver.getWindowHandles(). It gives you a list of all the open windows’ names.
18. How can you use Selenium to handle typing and mouse actions?
The Actions class in Selenium WebDriver lets you handle keyboard and mouse events. There are many ways to do things with the Actions class, such as click, double click, right click, drag and drop, mouse hover, and more.
You can use methods like `keyDown()`, `keyUp()`, and `sendKeys()` to do things with the keyboard. For instance, you can use `keyDown(Keys.SHIFT)` to simulate hitting a key and `keyUp(Keys.SHIFT)` to simulate letting go of it.
You can use `click()`, `doubleClick()`, `contextClick()`, `dragAndDrop()`, and `moveToElement()` to do things with the mouse.
For instance, you can use `moveToElement(element)` to make it look like the mouse is hovering over an element.
It is necessary to make an Actions object and then call the right method on that object in order to do these things. You need to call the `perform()` method to put the plan into action after you’ve defined it.
Keep in mind that not all websites and operating systems can handle all keyboard and mouse events. Depending on the browser and operating system, the exact behaviour may be different.
19. What are DesiredCapabilities used for in WebDriver?
In Selenium WebDriver, the DesiredCapabilities class is used to set the WebDriver’s features, such as the browser name, platform, and other capabilities. It is mostly used to set up drivers in a certain way or to test a WebDriver that is located far away.
That is, you can use DesiredCapabilities to choose the OS (Windows, Linux, etc.), the type of browser (Chrome, Firefox, etc.), and the version of the browser for your tests. It can also be used to set up features that only work in certain browsers. Chrome can be set to start in private viewing mode, and Firefox can be set to start in incognito mode.
DesiredCapabilities is also used in Selenium Grid, where tests need to be run on various computers using various browsers and systems. You can tell each test exactly what it needs to run so that it always works on the right computer.
It’s important to know that Selenium is getting rid of DesiredCapabilities. Instead, you should use the Options classes for each browser, such as ChromeOptions, FirefoxOptions, and so on. These classes give you a safer, easier-to-read way to set up the browser-specific features.
20, What can you do with Selenium WebDriver to deal with network latency?
To effectively manage network latency, you can utilize the driver.manage().timeouts().pageLoadTimeout() method. This method allows you to set a specific time frame for a page to load before it throws a TimeoutException.
21. How do you get an element’s CSS properties?
The `getCssValue()` method in Selenium WebDriver lets you get the CSS values of a web element. This method gets the value of a web element’s CSS feature that you give it.
To retrieve a CSS property using getCssValue(), follow these steps:
WebElement element = driver.findElement(By.id(“myId”));
String cssValue = element.getCssValue(“property-name”);
Change “property-name” to the name of the CSS property you want to get and “myId” to the real ID of your element in the code above. As a string, the `getCssValue()` method will return the value of the CSS field that was given.
Like, `element.getCssValue(“color”)` can be used to get the color of an element. This will give you the element’s color in RGB format.
Make sure you use the exact name that is given in the CSS. The property name is case-sensitive.
22. How does Selenium handle web features that change over time?
XPath can be used in Selenium to work with dynamic page elements. XPath has many functions, axes, and operators that can be used to make complicated expressions that work with dynamic elements.
23. How do you pick a number from a dropdown?
With the Select class in Selenium WebDriver, you can pick a number from a dropdown menu. There are ways to work with dropdown menus through the Select class.
Use it like this:
WebElement dropdown = driver.findElement(By.id(“myDropdown”));
Select select = new Select(dropdown);
Change “myDropdown” in the above code to the real ID of your dropdown element. There are three ways to choose a choice once you have an instance of the Select class:
- Using the select Method: You can use the select method to choose a choice from the options provided. This method allows you to specify the index of the choice you want to select.
Example: tselect.selectByVisibleText(“Option1”)
- Using the index Method: Another way to choose a choice is by using the index method. This method returns the index of the choice you want to select.
Example:`select.selectByValue(“1”)`
- Using the value Method: The third way to choose a choice is by using the value method. This method allows you to specify the value of the choice you want to select.
Example:`select.selectByIndex(1)`
Make sure that your menu is a select element before you use the Select class. You’ll have to find other ways to connect with it if it’s not.
24. In Selenium, how do you do a drag-and-drop action?
The motions class in Selenium WebDriver lets you do drag and drop motions. You can make an action that looks like drag and drop by calling the `dragAndDrop()` method on the Actions class.
Here’s a simple explanation of how to use the drag and drop functionality in Selenium WebDriver using Java:
First, you need to identify the source and target elements on the webpage using their respective locators (e.g., id, xpath, css selector, etc.). In this example, we’re using the id locator.
WebElement sourceElement = WebElement sourceElement = driver.findElement(By.id(“source”));WebElement targetElement = driver.findElement(By.id(“target”));
Next, you create an instance of the Actions class, which provides methods for performing advanced user interactions like drag and drop.
Actions actions = new Actions(driver);
Finally, you use the dragAndDrop() method of the Actions class to perform the drag and drop operation. This method takes the source and target elements as arguments.
actions.dragAndDrop(sourceElement, targetElement).perform();
The perform() method is called to execute the action.
This code will drag the element with the id “source” and drop it onto the element with the id “target” on the webpage.
Keep in mind that not all websites and operating systems can handle all keyboard and mouse events. Depending on the browser and operating system, the exact behavior may be different.
25. How does Selenium handle AJAX calls?
Since AJAX lets web pages be updated asynchronously by sharing data with a web server in the background, it can be hard to handle AJAX requests while using Selenium. Because of this, web elements may load at different times, and Selenium may try to deal with an element before it has loaded, which could lead to errors.
Selenium has classes called WebDriverWait and ExpectedConditions that you can use to handle AJAX calls. You can set the longest time that WebDriverWait will wait for a condition, and ExpectedConditions gives you a list of conditions that it will wait for.
Allow me to show you how to use it:
WebDriverWait wait = new WebDriverWait(driver, 20);WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“elementId”)));
Within this code, Selenium will wait up to 20 seconds for the “elementId” element to show up. The next step will be taken if the part can be seen within 20 seconds. This will result in a TimeoutException if it does not succeed.
With this method, you can wait for elements to load after an AJAX call, which makes your tests more reliable. But you need to be careful when using specific waits so that your tests don’t run more slowly.
26. How can Selenium deal with web features that change over time?
Changing XPath or CSS Selectors can be used in Selenium to work with changing elements. The methods contains(), starts-with(), and ends-with() can be used to make these locators. Another way to deal with dynamic parts in CSS Selectors is to use regular expressions.
27. What are some ways to handle browser movement in Selenium WebDriver?
The navigate() methods in Selenium WebDriver let you control how the browser moves. You can use to(String url) to go to a URL, back() to go back, forward() to go forward, and refresh() to make the current page look new again.
28. In Selenium WebDriver, how do you do the mouse hover action?
The Actions class in Selenium WebDriver lets you do mouse hover actions. You can fake the mouse hover effect with the `moveToElement()` method of the Actions class.
29. How do you use Selenium WebDriver with more than one window?
To manage multiple windows in Selenium WebDriver, you can use the `getWindowHandles()` and `switchTo().window()` methods. The `getWindowHandles()` method returns a set of window handles that you can use to iterate over all open windows. The `switchTo().window()` method allows you to switch between windows and focus on a specific one.
30. What is the best way to use JavaScript in Selenium WebDriver?
With the JavaScriptExecutor interface, you can use JavaScript in Selenium WebDriver. This interface gives you ways to run JavaScript code straight in your Selenium WebDriver.
31. What is the best way to handle page sliding in Selenium WebDriver?
With the JavaScriptExecutor interface, you can control how pages are scrolled in Selenium WebDriver. To scroll to a certain spot or element, you can use JavaScript methods like `window.scrollTo(x-coord, y-coord)` or `element.scrollIntoView()`.
32. What is the best way to use Selenium WebDriver to upload files?
To handle file uploads in Selenium WebDriver, use the `sendKeys()` method to send the absolute file path to the file input element.
33. What should you do when Selenium WebDriver shows a login pop-up?
To deal with Selenium WebDriver authentication pop-ups, you can either pass the account and password in the URL or use the Alert interface to talk to the pop-up.
34. How does Selenium WebDriver handle SSL certificates?
That’s easy to do with Selenium WebDriver. You can use the DesiredCapabilities class to set the browser-specific capabilities or the Options classes for each browser, such as ChromeOptions, FirefoxOptions, and so on.
35. How do I connect Selenium WebDriver to a server and use it?
You can set up Selenium WebDriver to work with a proxy in the DesiredCapabilities class or in the Options class for each browser.
36. How do you use the cloud to run Selenium WebDriver tests?
In the cloud, you can use cloud testing tools like Sauce Labs, BrowserStack, and more to run Selenium WebDriver tests. You can use the Selenium WebDriver APIs that these sites offer to run your tests on their servers.
37. How can four Selenium WebDriver tests be run at the same time?
Testing tools that allow parallel execution, such as TestNG, JUnit, and others, can be used to run Selenium WebDriver tests at the same time. Selenium Grid also lets you run your tests on different computers at the same time.
38. How do you connect Selenium WebDriver to other tools like Maven, Jenkins, and so on?
You can use the apps or APIs for other automation tools, like Jenkins, Maven, etc., to connect Selenium WebDriver to them. This is one way that the Maven Surefire Plugin can help you run your Selenium tests as part of your Maven build. The Jenkins Selenium Plugin can also be used to run your Selenium tests as part of your Jenkins build.
39. What is the best way to use Selenium WebDriver to handle CAPTCHA?
Because CAPTCHA is meant to stop automation, Selenium WebDriver doesn’t have a clear way to deal with it. You can get around this problem, though, by using third-party services that solve CAPTCHAs or asking your development team to turn off CAPTCHAs in the testing setting.
Conclusion
To increase your chances of getting hired as an automation tester, it’s essential to prepare well for Selenium developer interview questions. Understanding the basics of Selenium WebDriver is crucial. While theoretical knowledge is important, so is practical experience and hands-on skills. Keep practicing and improving your automation testing skills to boost your confidence and success in the interview.