As businesses embrace automation testing to streamline their web development process, the demand for skilled Selenium developers is rising. According to Enlyft, 78,921 companies use Selenium worldwide.
This makes Selenium a popular framework for the automated testing of web applications, and it supports a variety of programming languages, including Java, Python, C#, Ruby, and JavaScript. It also allows cross-platform compatibility, is an open-source language and can be easily integrated with other tools and frameworks like Jenkins, TestNG, and Cucumber, making it one of the most popular coding languages.
However, recruiters often encounter many challenges while recruiting top talent in this field. Shortage of skilled Selenium developers, varying levels of experience, and vetting and evaluating the skillsets are some of the challenges faced.
So, to hire top talent, recruiters and hiring managers must ask the right selenium coding interview questions.
In this article, we’ll explore 120+ selenium coding interview questions and answers to help you identify the most qualified candidates for the job.
In addition to technical skills, several soft skills are essential for Selenium developers to succeed. Here are some:
To make your life easier, we have conducted thorough research and curated a set of selenium coding interview questions and answers that you can utilize to simplify the process of interviewing developers.
1. What is Selenium, and what is it composed of?
Selenium is a suite of tools used for automated web testing. It is composed of the following:
2. How will you find an element using Selenium?
Every object in a web page is referred to as an element. There are various ways to find an element on a web page:
3. Explain what is an assertion in Selenium and what are the types of assertions?
An assertion is used as a verification point. It verifies the state of the application conforms to what is expected. The types of assertions are “assert,” “verify,” and “waifFor.”
4. Mention what the use of Xpath is?
XPath is used to find the WebElement in web pages. It is also helpful in identifying the dynamic elements.
5. Explain the difference between single and double slash in Xpath?
6. List out the technical challenges with Selenium?
The technical challenges with Selenium are:
7. What is the difference between type keys and type commands?
8. What are some advantages of Selenium?
The following are the advantages of Selenium: -
9. What are some limitations of Selenium?
Following are the limitations of Selenium: -
10. What is Automation Testing?
Automation testing is the software testing process using automation tools to find defects. In this process, automation tools perform the test scripts and generate the results automatically. Some most popular tools for automation testing are HP QTP/UFT, Selenium WebDriver, etc.
11. What are the benefits of Automation Testing?
12. What Is WebdriverIO?
WebdriverIO is a robust framework for testing mobile and web applications. It uses JavaScript to implement the Selenium Webdriver API and the wire protocol as per the W3C standards. It is available for installation as an NPM package and runs on Node.js. Hence, it is also famous as “Selenium 2.0 bindings for NodeJS”.
13. What are all testing types supported by Selenium WebDriver?
Selenium Webdriver can be used for performing automated functional and regression testing.
14. What are the different network protocols that Selenium supports?
HTTP and HTTPS are the two most commonly used network protocols. And in Selenium, there are multiple ways to handle both protocols.
15. Mention what the use of X-path is?
The WebElement is located on web pages using X-Path. It also enables the identification of dynamic elements.
16. How can we inspect the web element attributes to use them in different locators?
Using Firebug or developer tools, we can inspect the specific web elements. Firebug is a Firefox plugin that provides various development tools for debugging applications. From an automation perspective, firebug is used specifically for inspecting web elements to use their attributes, like id, class, etc., in different locators.
17. How can we find an element in Xpath by only partially matching the value of its attributes?
Using contains () method, you can locate an element by partially matching its attribute’s value. This is helpful in scenarios where the attributes have dynamic values with a specific constant part.
xPath expression = //*[contains(@name,'user')]
The above code will match name attribute values that contain the word 'user.'
18. What is the difference between verify and assert commands?
19. While using the click command, can you use screen coordinate?
The clickAT command is used to click on a specific part of an element. The element locator and x, y coordinates are accepted as arguments for the clickAt command (locator, cordString).
20. What are the four parameters to pass in Selenium?
The four parameters to pass in Selenium are:
1. How can we locate elements using their text in XPath?
Using the text() method: xPathExpression = //*[text()='username']
2. What is the fastest web driver implementation?
HTMLUnit Driver implementation is the quickest.
3. Explain how to assert the webpage's text using Selenium 2.0?
WebElement el = driver.findElement(By.id(“ElementID”))
String text = el.getText();
Assert.assertEquals(“Element Text”, text);
4. What is Object Repository?
An object repository is a necessary component of any UI automation. This is because it allows a developer to store all objects used in the scripts in one or more centralized locations rather than scattered throughout the test scripts.
5. How to send ALT/SHIFT/CONTROL keys simultaneously in Selenium WebDriver?
We hold down these keys and click other buttons to achieve unique functionality. To hold on to these keys, we need to define two more methods apart from keys.ALT or keys.SHIFT or keys.CONTROL functions. To include more than one key to be pressed or released, we can pass ALT, SHIFT, and CONTROL as parameters in the modifier key argument.
Here is the syntax for the same:
Actions a = new Actions(driver)
a.keyDown(Keys.ALT).keyDown(Keys.SHIFT) .keyDown(Keys.CONTROL).sendKeys('test') .build() .perform();
6. How can you upload a file using Selenium WebDriver?
We can upload a file using the element.sendKeys (file path) command. But before doing so, we must use the HTML tag ‘input’ with the attribute type as a file. This helps Selenium identify the web element first and upload the file.
7. How can you use regular expressions in Selenium?
Regexp is a keyword that can be prefixed to a text to treat it as a regular expression in Selenium.
8. How can you handle colors in a web driver?
We can use the command getCSSValue(arg0) to fetch the colors by sending the color as the argument.
9. Which command takes you forward by one page in the browser history?
The navigate().forward() command takes you forward by one page (previous page) on the browser’s history. This method lets the browser click the forward button in the existing browser window.
10. What is POM (Page Object Model)?
Every application webpage has a corresponding page class responsible for locating and performing actions on the web elements. POM is a design pattern that creates object repositories for web elements. It improves code reusability and readability. You can also run multiple test cases on the object repository.
11. How can you enable a disabled textbox?
We can enable a disabled HTML element by changing the attributes using the .removeAttribute(“disabled”) method.
Here is a code to allow a text box using the Javascript executor.
String enable ="doc.getElementsByName("name').removeAttribute('disabled');";
javascript.executeScript(enable);
12. How can we type text in a textbox element using Selenium?
We can type text in a textbox using the sendKeys() method.
Here is the code:
WebElement searchTextBox = driver.findElement(By.id("srch"));
searchTextBox.sendKeys("searchTerm");
13. How can you submit a form in Selenium?
We can submit a form in Selenium using the submit() method.
Here is the code:
driver.findElement(By.id("form1")).submit();
14. How do you take a screenshot with Selenium Webdriver?
We can use the TakeScreenshot function to capture the screenshot and getScreenshotAs() command to save the screenshot.
15. Which type command should you use if there is a reload event once your typing is completed?
The typeAndWait command can be used when a reload event in the software posts a typing action.
16. What’s the difference between close and quit commands?
The distinction between close and quit commands is as follows: -
17. How to delete cookies in Selenium?
Using the deleteAllCookies() method.
Here is the code:
driver.manage().deleteAllCookies();
18. Write the code to double-click an element.
Code to double-click an element: -
Actions action = new Actions(driver);
WebElement element=driver.findElement(By.id("elementId"));
action.doubleClick(element).perform();
19. How can we select elements by their attribute value using CSS Selector?
Using [attribute=value], we can select all elements of a specific class. For example, '[type=small]' will select the element with the attribute type of value'small'.
20. What is the fundamental difference between XPath and CSS selectors?
The primary distinction between XPath and CSS selector is that XPaths allow us to traverse up in the document, allowing us to move to parent elements. With CSS selectors, we can only move downwards in the document.
21. What is Desired capabilities of the selenium web driver?
Desired capabilities are a collection of key-value pairs used in browser instances to store or configure browser-specific properties such as version, platform, and so on.
22. How can we find all the links on a web page?
The anchor tag 'a' is used for all of the links. So, by looking for elements of tagName 'a,' we can find all of the links on a page.
Here is the code:
List<WebElement> links = driver.findElements(By.tagName("a"));
23. How can we capture screenshots in Selenium?
Using getScreenshotAs method of TakesScreenshot interface, we can take screenshots in Selenium
Here is the code:
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\testScreenShot.jpg"));
24. How can we check if an element is getting displayed on a web page?
The isDisplayed method enables us to check if an element is displayed on a web page.
driver.findElement(By locator).isDisplayed();
25. Can a captcha be automated?
No, a captcha and barcode reader cannot be automated.
26. What is an Object repository?
An object repository is a centralized location for all test script objects or WebElements. Using the Page Object Model and Page Factory design patterns in Selenium, we can create an object repository.
27. What is a hybrid framework?
A hybrid framework is a combination of one or more frameworks. It is usually associated with a combination of data-driven and keyword-driven frameworks where the test data and test actions are kept in external files (in the form of a table).
28. When do we use Selenium Grid?
We use a selenium grid when we want to run the same or different test scripts on multiple platforms simultaneously. This helps us test under various environments, and the loss of time consumed for testing is reduced.
29. What is a node in a selenium grid?
Nodes are machines that are linked to the Selenium grid hub and host Selenium instances that run the test scripts. The selenium grid, unlike the hub, can have multiple nodes.
30. Explain the line of code Webdriver driver = new FirefoxDriver();
Webdriver driver = new FirefoxDriver(), refers to creating an object of type WebDriver by implementing an object of the FirefoxDriver class.
1. How can we move to the nth child element using XPath?
Using XPath, there are two ways to get to the nth element:
2. How can the message in the alert box be retrieved?
The command storeAlert() can be used to retrieve the message from the alert pop-up and save it to a variable.
3. How can we move to the parent of an element using XPath?
Using the ‘..’ expression in XPath, we can move to the parent of an element.
4. Can Selenium handle pop-up windows?
Selenium is an automation testing tool that can only work with web applications. As a result, Selenium cannot handle the pop-up in windows. However, by integrating with third-party tools, we can overcome this issue.
5. What exactly is the distinction between getwindowhandle() and getwindowhandles()?
6. How can we launch different browsers in the selenium web driver?
By creating an instance of a driver of a particular browser- WebDriver driver = new FirefoxDriver();
7. What are regular expressions, and how can you use them in Selenium?
Regular expressions are special text strings that are used to represent search patterns. Regexp is a keyword that can be used as a prefix to treat a text as a regular expression in Selenium.
8. Write the code to right-click an element.
Code to right-click an element in Selenium: -
Actions action = new Actions(driver);
WebElement element=driver.findElement(By.id("elementId"));
action.contextClick(element).perform();
9. How do you confirm the precise position of a web element?
The commands verifyElementPositionLeft and verifyElementPositionTop are used. These make use of pixel comparison to determine the position of the element from the left and top of the web page.
10. How can you redirect browsing from a browser through some proxy?
Selenium facilitates a PROXY class to redirect browsing from a proxy. Look at the example below.
Example:
String PROXY = "199.201.125.147:8080";
org.openqa. Selenium.Proxy proxy = new.org.openqa.selenium.Proxy();
proxy.setHTTPProxy(Proxy)
.setFtpProxy(Proxy)
.setSslProxy(Proxy)
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);
11. How to type text in a textbox using Selenium?
The sendKeys(“String to be entered”) enter the string in a textbox.
Syntax:
WebElement username = drv.findElement(By.id("Email"));
// entering username
username.sendKeys("sth");
12. List the automation tools that can be integrated with Selenium to achieve continuous testing.
Here are some of the automation tools to achieve continuous testing:
13. What are the different types of annotations which are used in Selenium?
The different types of annotations used in Selenium include:
14. When do you make use of AutoIT?
Selenium is only used to automate web applications. However, if we want to handle, manage, or maintain the GUI, HTML pop-ups, we must use AutoIT.
15. In the Selenium web driver, which API is used for database testing?
Selenium employs JDBC (Java Database Connectivity) for database testing. This enables us to create and execute SQL (Structured Query Language) queries.
16. Can you explain how the web driver handles colors?
By passing the color as an argument to the command getCssValue(arg0), we can retrieve the colors.
17. Which web driver implementation is the fastest?
HTML Unit Driver is the fastest web driver implementation. This is because this driver does not run the tests in the browser but instead runs only plain HTTP, which is faster than expected.
18. What exactly are enhanced privilege browsers?
Privileges browsers are similar to Proxy Injection in that they allow websites to do things that are not normally permitted.
19. What is the distinction between a get request and a post request?
A get request retrieves data from a server, whereas a post request submits data to a server.
20. How can we determine the value of an element's attributes such as name, class, and value?
Using getAttribute(“{attributeName}”) method, we can find the value of different attributes of an element e.g.-
String valueAttribute = driver.findElement(By.id("elementLocator")).getAttribute("value");
21. What is an implicit wait in Selenium?
An implicit wait is a type of wait that waits for a specified time while locating an element before throwing NoSuchElementException. By default, selenium tries to find elements immediately when required without any wait. So, it is good to use implicit wait. This wait is applied to all the elements of the current driver instance.
Syntax:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
22. What expected conditions can be used in Explicit waits?
Some of the commonly used expected conditions of an element that can be used with explicit waits are: -
23. What is fluent wait in Selenium?
A fluent wait is a type in which we can also specify polling interval(intervals after which the driver will try to find the element) along with the maximum timeout value.
Syntax:
Wait wait = new FluentWait(driver) .withTimeout(20, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement textBox = wait.until(new
Function<webdriver,webElement>() {
public WebElement apply(WebDriver driver) { return driver.findElement(By.id("textBoxId")); }
}
);
24. What different keyboard operations can be performed in Selenium?
The different keyboard operations that can be performed in Selenium are:-
25. Write the code to double-click an element in Selenium?
Code to double-click an element in Selenium: -
Actions action = new Actions(driver);
WebElement element=driver.findElement(By.id("elementId"));
action.doubleClick(element).perform();
26. Write the code to right-click an element in Selenium?
Code to right-click an element in Selenium: -
Actions action = new Actions(driver);
WebElement element=driver.findElement(By.id("elementId"));
action.contextClick(element).perform();
27. How to mouse hover an element in Selenium?
Code to mouse hover over an element in Selenium-
Actions action = new Actions(driver);
WebElement element=driver.findElement(By.id("elementId"));
action.moveToElement(element).perform();
28. How can we fetch the title of the page in Selenium?
Using driver.getTitle(); we can fetch the page title in Selenium. This method returns a string containing the title of the webpage.
29. How can we fetch the page source in Selenium?
Using driver.getPageSource(); we can fetch the page source in Selenium. This method returns a string containing the page source.
30. How to verify tooltip text using Selenium?
Tooltips webelements have an attribute of type ‘title’. By fetching the value of the ‘title’ attribute, we can verify the tooltip text in Selenium.
Syntax:
String toolTipText = element.getAttribute("title");
1. How to execute javascript in Selenium?
JavaScript can be executed in Selenium using JavaScriptExecuter.
Sample code for javascript execution-
WebDriver driver = new FireFoxDriver();
if (driver instanceof JavascriptExecutor) { ((JavascriptExecutor)driver).executeScript("{JavaScript Code}"); }
2. How to handle hidden elements in Selenium webDriver?
Using JavaScript executor, we can handle hidden elements: -
(JavascriptExecutor(driver)) .executeScript("document.getElementsByClassName(ElementLocator).clic k();");
3. Name an API used for logging in Java.
Log4j is an open-source API widely used for logging in Java. It supports multiple levels of logging like - ALL, DEBUG, INFO, WARN, ERROR, TRACE, and FATAL.
4. Explain the difference between Borland Silk and Selenium is?
Borland Silk |
Selenium |
|
|
|
|
|
|
|
|
5. Mention why to choose Python over Java in Selenium?
Some advantages of using Python over Java with Selenium include:
6. Mention which Java API is required to generate PDF reports.
Java API IText is required to generate PDF reports.
7. What API is required for database testing in Selenium Webdriver?
JDBC (Java Database Connectivity) API is required for database testing in Selenium Webdriver. It enables you to run SQL statements.
8. What is the command that is used to display a variable's values in the output console or log?
The above uses PHP. If you use Java, replace echo with System.out.println
9. Explain how you can use a recovery scenario with Selenium?
The recovery scenarios are determined by the programming languages you use. If you're using Java, you can get around this by using exception handling by incorporating a "Try Catch Block" into your Selenium WebDriver Java tests.
10. Explain how to iterate through options in the test script?
To iterate through options in the test script you can loop features of the programming language. For example, to type different test data in a text box, you can use the “for” loop in Java
// test data collection in an array
String[ ] testData = { “test1” , “test2” , “test3” } ;
// iterate through each test data
For (string s: test data) {Selenium.type (“elementLocator”, testData) ; }
11. In Selenium IDE, how can you generate random numbers and dates for test data?
In Selenium IDE, you can generate random numbers by using Java Script:
12. What is selenium RC (Remote Control)?
In terms of browser and language support, Selenium IDE has limitations. These limitations can be reduced by using Selenium RC: -
13. Explain what the main difference between webdriver and RC is?
The main difference between Selenium RC and Webdriver is that selenium RC injects javascript function into browsers when the page is loaded. On the other hand, Selenium Webdriver drives the browser using built-in browser support.
14. Explain how you can capture server-side log Selenium Server?
To capture server side log in Selenium Server, you can use the command: -
java –jar .jar –log selenium.log
15. How to execute JavaScript code in Selenium?
JavaScript code can be executed in Selenium using JavaScriptExecuter. Here’s a sample code: -
WebDriver driver = new FireFoxDriver();
if (driver instanceof JavascriptExecutor) {
((JavascriptExecutor)driver).executeScript("{JavaScriptCode}");
}
16. What is the use of the testng.xml file?
A testng.xml file is used for configuring the whole test suite. In this file, we can create a test suite, create test groups, mark tests for parallel execution, add listeners, and pass parameters to test scripts. Later, this testng.xml file can be used for triggering the test suite.
17. How can we make one test method dependent on others using TestNG?
Using the ‘dependsOnMethods’ parameter inside @Test annotation in TestNG, we can make one test method run only after the successful execution of the dependent test method.
Syntax:
@Test(dependsOnMethods = { "preTests" })
18. How can we set the priority of test cases in TestNG?
Ans. Using the priority parameter in @Test annotation in TestNG, we can define the priority of test cases. When not specified, the default priority of the test is the integer value 0. Example: -
@Test(priority=1)
19. What are the commands for downloading Selenium in python?
20. How can we launch Different browsers using python?
1. What is Selenium?
Selenium is a robust test automation suite that is used for automating web-based applications. It supports multiple browsers, programming languages, and platforms.
2. What are the different components of Selenium?
Selenium has four components-
3. What is an XPath?
Xpath or XML path is a query language for selecting nodes from XML documents. XPath is one of the locators supported by selenium web driver.
4. What is an absolute XPath?
An absolute XPath is a way of locating an element using an XML expression beginning from the root node, i.e., html node in the case of web pages. The main disadvantage of absolute xpath is that even with the slightest change in the UI or any element, the whole absolute XPath fails.
Example - html/body/div/div[2]/div/div/div/div[1]/div/input
5. What are various ways of locating an element in Selenium?
The different locators in selenium are-
1. Id
2. XPath
3. cssSelector
4. className
5. tagName
6. name
7. linkText
8. partialLinkText
6. Mention what is Selenium 3.0?
Selenium 3.0 is the latest version of Selenium. It has released 2 beta versions of selenium 3.0 with a few of the below changes:
7. How can we check if an element is enabled for interaction on a web page?
Using the isEnabled method, we can check whether an element is enabled.
Syntax: - driver.findElement(By locator).isEnabled();
8. What is a soft assertion in Selenium? How can you mark a test case as failed by using soft assertion?
Soft Assertions are customized error handlers provided by TestNG. Soft Assertions do not throw exceptions when an assertion fails, and they continue to the next test step. They are commonly used when we want to perform multiple assertions.
To mark a test as failed with soft assertions, call the assertAll() method at the end of the test.
9. What is Robot API?
Robot API is used for handling Keyboard or mouse events. It is generally used to upload files to the server in selenium automation.
Robot robot = new Robot();
//Simulate enter key action robot.keyPress(KeyEvent.VK_ENTER);
10. How to handle HTTPS websites in Selenium? or how to accept an untrusted SSL connection?
Using profiles in Firefox we can handle accepting the SSL untrusted connection certificate. Profiles are a set of user preferences stored in a file.
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
WebDriver driver = new FirefoxDriver(profile);
1. Describe a situation where you had to explain a technical concept to a client with limited technical knowledge. How did you approach this situation, and what was the outcome?
2. How do you handle disagreements with a team member?
3. How do you ensure everyone on the team knows a project’s progress?
4. What’s your approach to collaborating with someone outside your team to solve a problem?
5. How do you handle conflicts within your team? Can you give an example of how you have resolved any disputes in the past?
6. Time management has become an essential factor in productivity. Give an example of a time-management skill you’ve learned and applied at work.
7. What projects have you worked on, and how are they relevant to our company’s environment?
8. How do you prioritize tasks with multiple issues or bugs to fix?
9. What’s your approach to handling challenging technical projects?
10. Can you describe a time when you had to adapt to a change in project requirements? How did you handle the situation?
During an interview, you have a limited amount of time to gain insight into a candidate's qualifications, background, and character. It is essential to ask the right questions to obtain the most important information. Here are a few tips to help you gather the insights required to hire qualified candidates.
Here are a few ways iMocha can help assess the skills of Selenium developers:
Formulating the right selenium coding interview questions is integral to the recruitment process. However, companies must also focus on reducing the time-to-hire and increasing the accuracy of candidate evaluations.
This is where an intelligent skills platform like iMocha comes in. By leveraging technology, iMocha provides actionable insights so you can ensure to hire of talented candidates in the shortest time possible. Ultimately, it leads to an organization's success and ability to attract and retain top talent.
1. How would you explain the Selenium framework in an interview?
Selenium is an open-source automation testing framework. It is widely used for web applications. With Selenium, developers can automate web browsers across multiple platforms that support languages like Java, Python, C#, Ruby, etc. This framework comprises three main components: Selenium WebDriver, Selenium Grid, and Selenium IDE.
2. What are the four parameters in Selenium?
There are four parameters in Selenium. These include:
3. How to handle dropdowns in Selenium?
Dropdowns in Selenium can be handled using the Select class. Here are the steps:
4. What is POM in Selenium?
POM stands for "Page Object Model." Its design pattern is used to organize code for automated testing of web applications. In POM, each web page is represented by a Java class called a "Page Object." This Page Object comprises methods and elements used to interact with web pages.