Are you preparing for an upcoming interview for a Selenium testing job? With the growing demand for Selenium testers, it’s important to have a thorough understanding of the key concepts and skills required for the role. In this article, we will provide you with a comprehensive list of the top 10 Selenium interview questions that are commonly asked in interviews. We’ll cover topics such as Selenium WebDriver, test automation, test frameworks, and more
Contents
1. Difference between driver.quit() and driver.close() ?
The two commands driver.quit() and driver.close() are basically used to close a browser window/session. Here is a detailed explanation about each of the command:
- driver.quit() is a command that closes the complete webdriver session . It will close all the popups , tabs and releated windows irrespective of the window in focus.
- driver.close() is a command that closes only the particular window which is in focus. If there is only one window then it closes the entire webdriver session.
2. Difference between driver.naviagte().to() and driver.get()?
The two commands driver.navigate().to() and driver.get() are basically used to the same operation that is to open a webpage and navigate to a particular URL. Here is a detailed explanation about each of the command:
- driver.get() is a command that helps us navigate to a particular URL . This command waits for the entire webpage to load before returning the control back . The only drawback with this method is that it does not store the browser history and cookies.
- driver.navigate().to() is also a command that helps us navigate to a particular URL . Unlike driver.get() the navigate() method creates instance of the Navigation class which provides us with additional functionalities such as navigate.refresh(),navigate.back(),navigate.forward() etc . In simple terms it can store the browser history as well.
It Should be noted that behind the scenes driver.navigate.to() is using the driver.get() method itself.
3. What are the different exceptions that can occur in Selenium ?
Exception : Exception is a unwanted event or issue that occurs during the execution of a program which disturbs the normal flow of program’s steps.
These are the most common exceptions that can occur during execution of a selenium program:
- NoSuchElement Exception
- Stale Element Reference Exception
- Element Click Intercepted Exception
- ElementNotVisible Exception
- NoSuchFrame Exception
- NoAlertPresent Exception
- NoSuchWindow Exception
- SessionNotFound Exception
4. What are the different ways in which waits can be added in Selenium?
There are basically three different types of waits that selenium library provides us with. Here is a detailed description about them :
Implicit Wait: Implicit wait in selenium is wait that informs the webdriver to wait for a certain period of time for webelement before throwing a “NoSuchElement Exception”.
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Explicit Wait: Explicit wait in selenium is wait that informs the webdriver to wait till a expected condition for a web element before throwing a “Element Not Visible Exception”. This type of wait is mainly used for dynamically loaded webpages.
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='primary']"));
Fluent Wait: Fluent wait in selenium is also considered to be a type of explicit wait .The only difference between explicit and fluent wait is that unlike in explicit wait in fluent wait this wait informs the webdriver to check for a expected condition with a certain frequency until a particular timeout occurs.
Wait wait = new FluentWait(driver)
.withTimeout(10, TimeUnit.SECONDS)
.pollingEvery(2, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
5. How do handle dropdowns in Selenium?
In Selenium basically we usually encounter select based dropdowns . These type of dropdowns are handled using the select class from the selenium package.
The select class is a ordinary class but to handle select dropdowns in Selenium, you can use the Select class provided by the Selenium WebDriver. The Select class provides methods to interact with dropdown elements, such as selecting options by their visible text, value, or index, as well as deselecting options.
// Create a new instance of the Select class
Select dropdown = new Select(driver.findElement(By.xpath("//div[@class='ui dropdown']"));
// Select an option by visible text
dropdown.selectByVisibleText("Open...");
// Select an option by value attribute
dropdown.selectByValue("option_value");
// Select an option by index (0-based)
dropdown.selectByIndex(1);
6. What is Actions class in Selenium?
Actions class is a special class provided by Selenium library which is used to provide us unique methods such as keyboard and mouse actions on the web elements such as double click , context click , drag and drop a element and much more. Using Actions and action we can chain multiple actions together in a sequence.
#Create a instance of Action class
Actions act = new Actions(driver);
#Instantiate a action using the above actions object
Action btnClick = act.contextClick(By.xpath("//div[@class='entry-content single-content']")).build();
#Perform the Action
btnClick.perform();
7. what is a XPath and what are the types of XPaths in Selenium?
XPath (XML Path Language) is a powerful language used to navigate through XML documents and identify elements in an XML structure. In the context of Selenium, XPath is widely used to locate elements on a web page. It provides a way to traverse the HTML structure and identify elements based on their attributes, tags, text content, or relative position in the document
Using XPath all the different elements in the webpage are located and the related selenium operations are done on them.
All the XPaths can be categorized into the following two categories:
- Absolute XPath
- Relative XPath
8. How are IFrames dealt in Selenium?
Iframe is a inline html code or webpage of a different webpage in the current webpage. These iframes are added to the current page using a <iframe> tag . These iframes are usually used to display ads or videos .
In order to test these or do some selenium operations on these iframes we need to switch to this iframe just like we switch window or tabs. In selenium there are three ways in which we can switch to a iframe.
- By using iframe id
- By using iframe name
- By using iframe web element
driver.switchTo().frame(name/id/webelement);
If we want to switch back to the parent frame or default context we can follow the below two commands to do the operation.
driver.switchTo().parentFrame();
driver.switchTo().defaultContext();
9. Explain the concept of headless browser testing in Selenium?
Headless browser in simple terms is basically a browser object with no User Interface or GUI . Using Headless mode we can test the browser by controlling it programmatically without the UI shown to the user .This type of testing is very helpful when you have to test multiple windows at once or run the test cases at a much faster pace. It should be noted that just like in normal UI testing every actions such as clicking a button , downloading files etc. are done behind the scenes without UI.
10. What is the difference between getWindowhandles() and getwindowhandle()?
Here is a detailed description on getWindowHandle() and getWindowHandles() methods and what they are used for:
- driver.getWindowHandle() – getWindowHandle() is a method that basically fetches the handle of the window in focus which is a unique identifier. The return type of this method is a string. It is used to get the address of the current browser.
- driver.getWindowHandles() – getWindowHandles() is a method that basically fetches the handles of all the open pages simultaneously.The return type of this method is a Iterator<string>.It is used to get all addresses of the open browsers.
To excel in a Selenium testing job interview, focus on showcasing your Selenium expertise, test automation knowledge, programming skills, understanding of web technologies, testing concepts, problem-solving abilities, collaboration skills, and commitment to continuous learning. Thanks, Yashwanth for the wonderful post! very useful for the new developers in the market preparing for it!