Since software testing is becoming more and more automated, there is a growing demand for Selenium professionals. It is essential to be proficient in Selenium and programming languages like Python or Java. Selenium experts play a critical role in increasing productivity and decreasing manual testing efforts, which makes them highly sought after as companies prioritize quality assurance and speedier delivery. Utilize this collection of top 40 questions for Selenium interview along with answers. Get started with our Selenium course syllabus.
Selenium Interview Questions for Freshers
Here are the frequently asked Selenium interview questions and answers.
1. What is Selenium?
Selenium is an open-source web application automation testing tool. Numerous programming languages and browsers are supported. With Selenium tool, testers can create automated tests in Python, C#, Java, and other computer languages.
2. What are the components of Selenium?
The major components of Selenium are Selenium Grid, Selenium RC (Remote Control), Selenium WebDriver, and Selenium IDE.
- Selenium Grid: A component that enables concurrent test running across numerous browsers and computers.
- Selenium RC: Selenium Remote Control.
- Selenium WebDriver: One well-liked component for running test scripts on browsers.
- Selenium IDE: An integrated development environment.
3. What is Selenium WebDriver?
WebDriver is a framework for browser automation that works directly with the browser to carry out tasks like typing and clicking.
An open-source program called Selenium WebDriver automates browser interactions. It is employed to test web apps in various browsers.
- Selenium WebDriver makes sure a website works the way it should.
- It is a well-liked open-source program that is comparatively simple to use.
- The Browser Testing and Tools Working Group of the W3C has recommended it.
4. What is the difference between Selenium RC and WebDriver?
In contrast to WebDriver, which communicates directly with the browser, Selenium RC needs a server to do so.
Factors | Selenium RC | WebDriver |
Architecture | It needs a different Selenium server to serve as a proxy between the browser and the test script. | It uses native browser drivers to communicate with the browser directly. |
Interaction | It transmits commands to the Selenium server, which converts them into native code for the browser. | It removes the need for a separate server by using the driver to send commands straight to the browser. |
Performance | Because of the additional server layer, it is thought to be slower and more complicated to set up. | It has quicker performance, easier to use, and compatibility with more browser features. |
Approach | It is a traditional approach. | It offers a more modern and streamlined approach to browser automation. |
Recommended: Selenium Online Course Program.
5. What programming languages does Selenium support?
Java, JavaScript, C#, Python, Ruby, PHP, and other programming languages are supported by Selenium. Among Applitools users, Java appears to be the most popular, followed by JavaScript.
6. What browsers does Selenium support?
ChromeDriver, GeckoDriver, Microsoft Edge WebDriver, SafariDriver, and InternetExplorerDriver are among the browser drivers that Selenium supports.
7. What is the use of findElement() and findElements()?
Whereas findElements() produces a list of all matching elements, findElement() delivers the first matching element.
In Selenium WebDriver, “findElement()” is used to locate and interact with a single web element on a page, while “findElements()” is used to locate and retrieve a list of all matching web elements on a page.
Key Factors | findElement | findElements |
Return Value | It returns the first element that matches. | It returns a list of every element that matches, even if none do. |
Exception Throw | If no matching element is discovered, an exception (“NoSuchElementException”) is thrown. | If no matching elements are discovered, an empty list is returned. |
Use Cases | It is helpful for interacting with a special element on the website. | It is helpful for iterating through several related components on a page. |
Kickstart your career with our software testing training in Chennai.
8. What are locators in Selenium?
Identifiers known as locators are used in Selenium to assist automated testing in locating and interacting with web page items. They are employed to carry out tasks including text entry, button clicking, and element presence validation.
Locators, such as ID, Name, ClassName, XPath, and CSS Selector, are used to identify elements on a web page.
9. What is XPath?
An XML or HTML document’s elements can be found using the XPath language.
An expression language called XPath was created to facilitate XML document manipulation and querying. It can be used to calculate values from an XML document’s content and was established by the World Wide Web Consortium in 1999.
10. What is the difference between absolute and relative XPath?
Relative XPath begins at any node in the document, but absolute XPath begins at the root node.
Absolute XPath: It provides the whole path to the element starting at the HTML document’s root. It is less adaptable and may break if the structure of the page changes.
Relative XPath: It is starting from a certain element, Relative XPath finds the requested element by navigating through the DOM hierarchy.
11. How do you handle dropdowns in Selenium?
There are various ways to construct drop-down menus on a website. The <select> HTML element is used to generate some dropdowns, whereas the <ul>, <li>, <button>, and <div> tags are used to make others.
The following techniques can be used to handle dropdown in Selenium.
- Method 1: By iterating through a list that contains all of the possibilities.
- Method 2: Without iterating the list, a custom locator is created.
- Method 3: Using the JavaScriptExecutor class.
- Method 4: By employing the sendKeys technique.
- Method 5: By utilizing the Actions Class.
Example: Select select = new Select(driver.findElement(By.id(“dropdown”)));
12. What is the difference between driver.close() and driver.quit()?
Whereas driver.quit() terminates the WebDriver session and closes all windows, driver.close() closes the active window.
Only the window that Selenium is using to execute automated tests is closed by close(). But the WebDriver session is still running. The driver.quit() method, on the other hand, terminates the WebDriver session and closes all browser windows.
13. How do you handle alerts in Selenium?
In Selenium, you can handle alerts by switching to the alert window using the switchTo().alert() method. The Alert Interface methods can be used to accept, dismiss, or retrieve the text once you’re in the alert window.
Steps to handle alerts in Selenium:
- To navigate to the alert window, use the switchTo().alert() function.
- To take action on the warning, utilize the warning Interface methods.
driving.switchTo().alert() functions such as getText(), dismiss(), and accept().
14. What is the difference between get() and navigate().to()?
The primary distinction is that each time the navigate API is used, cookies are kept. The traverse() method maintains the session state, in contrast to the get() method, which clears it each time it is invoked. While both are used to open a URL, navigate().to() permits back/forward navigation history.
Gain expertise with our manual testing course in Chennai.
15. How do you handle frames in Selenium?
You can use the SwitchTo().frame function in Selenium to manage frames. The browser can operate with numerous iframe components using this way.
How to switch to a frame:
- To switch to the frame, use its index. An iframe’s index begins at 0.
- To switch to a frame, use its name or ID.
- To switch to it, use the frame’s WebElement.
Examples:
- driver.switchTo().frame(1) Switches to the frame with index 1
- driver.switchTo().frame(“iframe1”) Switches to the frame with ID “iframe1”
- driver.switchTo().frame(WebElement frameElement) Switches to the frame with the WebElement frameElement
16. What is implicit and explicit wait?
Whereas explicit wait uses WebDriverWait to wait for a specified circumstance, implicit wait is applicable generally.
Implicit wait
- A global delay that affects every script element.
- waits for items to occur for a predetermined period of time.
- If the element is not discovered at that time, an exception labeled “no such element” is raised.
- There is no way to change the polling time; it is fixed.
Explicit wait
- A specific wait that is applicable to particular components or circumstances.
- It awaits the fulfillment of a certain need, such as the appearance, clickability, or presence of an element.
- It polls the program for a particular condition using loops.
- The code will generate a timeout error if the condition is not satisfied before the specified timeout value.
17. How do you take a screenshot in Selenium?
The steps to take a screenshot with Selenium are as follows:
- Make a class. Put TestNG ‘ITestListener’ into practice.
- Invoke the ‘onTestFailure’ function.
- To take a screenshot using this method, add the code.
- Obtain the name of the test method, then take a screenshot including the test name. After that, put it in the folder of your choice.
Using the UI TakesScreenshot:
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File(“screenshot.png”));
Fine-tune your testing skills with our manual testing interview questions and answers.
18. What is the difference between assert and verify?
In test suites, assert and verify are both used to compare expected and actual values to validate conditions. The primary distinction between the two is that although verify permits the test to proceed in the event that a condition fails, assert halts the test’s execution. If the condition fails, assert halts the test, whereas verify keeps it running.
Assert:
- It is used to find problems in software logic.
- It evaluates and contrasts expected and actual values.
- It determines whether a condition is true.
- The test case fails and execution halts if the condition is false.
Verify:
- It evaluates and contrasts expected and actual values.
- It permits the test to proceed even in the event that the verification is unsuccessful.
- It reports mistakes at the test’s conclusion.
19. How do you handle multiple windows in Selenium?
Using window handles to switch between windows and tabs is one way to manage many windows in Selenium. Tasks like button clicks, form filling, and page navigation can be automated with Selenium.
- Open a new window: To launch a new window, initiate an action, such as pressing a button.
- Get the current window handle: Use the driver to save the current window’s handle.getWindowHandle().
- Get all window handles: Make use of the driver.To obtain a collection of handles for every window that is open, use getWindowHandles().
- Switch to a new window: Make use of the driver.switchTo().handle to navigate to the new window.
- Perform actions: Engage with the new window’s elements.
- Switch back to the original window: To return to the original window, use the saved handle.
- Close the window: Make use of the driver.To end the active window, use close().
Explore our loadrunner interview questions and answers.
20. What is the use of Actions class?
It is employed for sophisticated user operations such as mouse hover, double-click, and drag-and-drop.
Testers can automate sophisticated user behaviors within a web application by using Selenium WebDriver’s Actions class to simulate complex user interactions like mouse movements, keyboard actions, clicks, and drag-and-drop operations on a web page. This effectively replicates how a real user would interact with the website.
Key Points of Action Class:
- Advanced Interaction.
- Chaining Actions.
- Essential for Testing.
21. What is a Page Object Model (POM)?
POM is a design pattern in which elements are described as methods and each web page is represented as a class.
Each web page of an application is represented as a class in the code using a Page Object Model (POM), a design pattern used in Selenium test automation.
This makes it possible to store web elements and their interactions in an organized manner and makes test scripts more reusable and maintainable by separating the page-specific details from the test logic itself.
Review your skills with our Python interview questions and answers.
22. What is the difference between POM and Page Factory?
The Selenium design pattern called Page Object Model, or POM, builds an object repository to hold all web elements. It enhances test case management and lessens code duplication.
Every web page in an application is regarded as a class file in the Page Object Model. There will only be matching web page elements in each class file. These components allow testers to manipulate the website they are testing.
Some of the differences between POM and Page Factory are as follows:
Page Object Model (POM) | Page Factory |
It is a design pattern approach. | Selenium Webdriver is the provider of this class. |
It is not the best way to handle tasks. | It is the best way to handle tasks. |
It facilitates the separation of scripts and page objects. | It is a method for putting POM into practice. |
Page objects are defined using the “By” annotation. | It describes page objects using the annotation “FindBy.” |
Exceptions are not handled effectively by it. | It manages the exceptions effectively. |
It necessitates initializing each object. | It is not necessary to initialize each and every object. |
Cache storage is available for tasks. | Cache storage is not required. |
23. How do you handle dynamic elements in Selenium?
The JavaScript executor, dynamic selectors, and waits can all be used to manage dynamic elements in Selenium. Moreover, window handles and the Alert interface for pop-ups can be used to navigate between windows. By employing explicit waits, CSS selectors, or relative XPath.
- Implicit waits: Waiting for an element to become accessible is done.
- Explicit waits: Waiting for dynamic elements is done.
- Fluent waits: Using fluid waits to manage dynamic aspects.
24. What is headless browser testing?
One method for doing automated tests on online applications without a graphical user interface (GUI) is headless browser testing. Because it does not require rendering images or Hypertext Markup Language (HTML), it is quicker and more effective than typical browser testing. Testing without a graphical user interface (e.g., headless Chrome).
25. How do you handle cookies in Selenium?
In your browser, you may manage cookies by allowing, blocking, or removing them. Additionally, you may control cookie settings for particular websites.
driving.manage().addCookie, deleteCookie, and getCookies().
How to handle cookies in Chrome:
- Open Chrome.
- Select Settings (three-dotted button).
- Select Privacy and Security.
- Select Cookies and other site data.
- Select Allow All Cookies or Block Third-party cookies.
How to handle cookies in Firefox:
- Open Firefox.
- Select Options.
- Select Privacy & Security.
- Go to the Cookies and Site Data section.
- View and delete individual cookies.
26. What is TestNG?
TestNG is a Selenium testing framework that facilitates parallel execution, grouping, and annotations. An open-source framework called TestNG is used to create and execute automated tests for Java applications. Inspired by JUnit and NUnit, it was developed by Cedric Beust.
Features of TestNG:
- Annotations: TestNG offers annotations that let users specify how tests should be conducted. The @BeforeGroups annotation, for instance, executes code prior to a group’s initial test case.
- Grouping: TestNG enables users to put tests together that have comparable features.
- Parameter Injection: Passing parameters to tests is made possible via TestNG’s support for parameter injection.
- Test Reports: Comprehensive test reports can be produced by TestNG.
- Prioritization: TestNG lets users rank tests in order of importance.
- Sequencing: To execute tests in a particular order, TestNG provides sequencing approaches.
27. How do you perform database testing with Selenium?
By connecting to the database and running queries using JDBC or ODBC integration. Since Selenium doesn’t support database interaction directly, you must incorporate a separate database connection mechanism (such as JDBC or ODBC) into your Selenium test scripts in order to conduct database testing with Selenium.
In other words, you use Selenium to manipulate the user interface (UI) in order to interact with the application, and then you use database queries in your test code to confirm the corresponding data changes in the database.
Explore our loadrunner training in Chennai.
28. What is the use of DesiredCapabilities?
Version, platform, and settings are among the browser-specific properties that can be set with it.
In order to customize the testing environment for cross-browser compatibility and particular test scenarios, you can use the “DesiredCapabilities” class in Selenium WebDriver to define a set of key-value pairs that specify the desired browser configuration for your test.
In other words, it tells Selenium which browser and environment to use when running your automated tests.
29. How do you handle SSL certificates in Browser?
To accept unsafe certificates, use DesiredCapabilities.
- Most modern browsers automatically handle the validation process in the background, displaying warnings if there are issues with the certificate.
- The main way to handle SSL certificates in a browser is to check the padlock icon in the address bar to know if a website is secure (HTTPS).
- If you encounter an error, you can review the certificate details to determine whether to trust the connection.
30. What is a Selenium Grid?
Selenium Grid enables concurrent execution of tests across several computers and web browsers.
A testing tool called Selenium Grid simultaneously executes automated tests on several computers and web browsers. It is a component of the Selenium toolkit and is used to test web apps in various browsers and OS systems.
31. Write code to click a button using XPath.
The code to click a button using XPath in Selenium:
driver.findElement(By.xpath(“//button[@id=’submit’]”)).click();
32. How do you scroll down a page in Selenium?
Using the JavaScript executor, you may run the “window.scrollBy(0, y)” command in Selenium to scroll down a page. “y” is the number of pixels you wish to scroll down; for example, to scroll down 500 pixels, you would use:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“window.scrollBy(0, 500);”);
33. Write code to handle a dropdown and select a value.
The Selenium code to handle a dropdown and select a value:
Select select = new Select(driver.findElement(By.id(“dropdown”)));
select.selectByVisibleText(“Option 1”);
34. How do you handle a file upload in Selenium?
One popular automation testing tool for testing web browsers is Selenium. Several classes or interfaces are available in the Java programming language to carry out file manipulation tasks. As with file handling in Selenium, the file manipulation action is referred to as import and export of files.
driver.findElement(By.id(“fileUpload”)).sendKeys(“path/to/file.txt”);
35. Write code to switch to a new window.
The code to switch to a new window:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
36. How do you handle a calendar widget in Selenium?
To choose the preferred date from the calendar, use XPath or CSS. Calendar controls can also be handled in a variety of ways.
Method 1: Use XPath or CSSSelector to locate the dates, then click on them directly.
Method 2: JavaScriptExecutor can also be used to set the date value directly in the date field.
37. Write code to perform a mouse hover action.
The Selenium code to perform a mouse hover action:
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.id(“element”))).perform();
38. How do you handle a StaleElementReferenceException?
To make sure the element is there, either move it or use an explicit wait. The standard method for handling a StaleElementReferenceException in Selenium is to use a try-catch block to catch the exception and then refresh the element reference inside the catch block to relocate the element on the page.
This is frequently done in combination with explicit waits to make sure the element is available before interacting with it once more.
39. Write code to handle a drag-and-drop action.
The Selenium code to handle a drag-and-drop action is below:
Actions actions = new Actions(driver);
actions.dragAndDrop(sourceElement, targetElement).perform();
40. How do you handle a timeout exception in Selenium?
Either use an explicit wait with the appropriate requirements or extend the wait time. In Selenium, a TimeOut Exception mostly happens when a condition is not satisfied within the allotted time. In order to ensure that the webpage loads fully and all web elements are accessible, one method to deal with timeout exceptions is to modify the wait time based on the behavior of the application.
Explore all our software training courses.
Conclusion
This collection of top 40 frequently asked questions for Selenium interview with answers span a broad spectrum of subjects, from fundamental to complex. To ace your Selenium interview, practice writing code and fully comprehend the fundamentals! Hone your skills with our Selenium training in Chennai.