Selenium is a leading industry-standard tool for web automation. Many companies prefer candidates with Selenium skills, making it a valuable asset for software testers. So below is the list of top Selenium Interview questions and answers that interviewers commonly ask. After conducting in-depth research and consulting with industry experts who are directly involved in the hiring process, MindMajix’s experts have compiled these questions.
Selenium is an open-source automation testing tool that has set the benchmark in the testing industry for a long time now. It's primarily used by software testers to automate browsers and web applications across various platforms. If you're looking for a thriving career in the testing domain, then Selenium is the wise option, as professionals in this field are in great demand.
We have categorized Selenium Interview Questions - 2024 (Updated) into two levels they are:
To prepare you well for your next Selenium Interview, we have collected some of the most frequently asked Selenium Interview Questions from our alumni trainees, that set you apart in the interview process.
So, let's have a look at them.
A test scripting language specially used by Selenium for creating test cases is called Selenese. It's a cross-platform language used for representing Selenium commands. By using Selenese, we can perform various actions like checking links, different UI elements, dropdown lists, etc. Besides, we can also perform actions like testing Ajax functionality, scrolling through a page, and a lot more web application features.
Mainly Selenium commands are of three types:
If you want to enrich your career and become a professional in Selenium, then enrol in "Selenium Training," This course will help you to achieve excellence in this domain. |
[ Learn Selenium Tutorial For Beginners ]
Types of testing supported by Selenium are as follows:
Selenium's latest version is Selenium 4.22.0 Alpha 5, which is released in June 2024.
Selenium 4.0 comes with new standardization and offers a seamless experience.
The key features of Selenium 4.0 Alpha 5 are:
[ Check out How to Download and Install Selenium? ]
The below command finds the element using link text and then clicks on that element, then after the user will be redirected to a corresponding page.
driver.findElement(By.linkText("Today's deals")).click();
The below command finds the element based on the substring of the link in the parenthesis and partial link text() finds the web element.
driver.findElement(By.partialLinkText("Service")).click();
Using JavaScriptExecutor, we can enter text in the text box.
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('email').value="123.ab@xyz.com");
Stale means old and no longer new. A stale element means an old element. Any element present on the webpage is considered as a web element in Web Driver. If the document object model (DOM) changes, then the web element goes stale. If we try to connect with any element that is staled, then StaleElementReferenceException is thrown.
The reasons for StaleElementReferenceException thrown are, one is a web element deleted entirely and the next id element no longer attached to DOM.
To handle StaleElementReferenceException, follow the below-mentioned ways:
Solution 1: Refresh the page and try again for the same element
Sample code to overcome the issue:
driver.navigate().refresh();
driver.findElement(By.xpath("xpath here")).click();
Solution 2: Use try-catch block within for loop, if the element is not found in DOM.
// Using for loop, it tries for 4 times.
// If the element is located for the first time then it breaks from the for loop nad comeout of the loop
for(int i=0; i<=3;i++){
try{
driver.findElement(By.xpath("xpath here")).click();
break;
}
catch(Exception e){
Sysout(e.getMessage());
}
}
Solution 3: Wait for element till it gets available
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("table")));
Use ExpectedConditions.refreshed to avoid StaleElementReferenceException and retrieve the element again. This element updates the element by redrawing it accessing the referenced element.
wait.until(ExpectedConditions.refreshed(ExpectedConditions.stalenessOf("table")));
Solution 4: Handle StaleElementElement Exception by POM.
[ Check out Top Selenium Projects and Use Cases ]
The same-origin policy is a critical security mechanism that defines how the script or document loaded from one origin can associate with a resource from another origin. It helps to isolate potential malicious documents and rescue from attack vectors.
For example, for a URL http://www.google.com/resources/, the origin is a combination of a google.com, http, 80 correspondingly. Selenium Core (JavaScript program) cannot obtain the elements from an origin that is different from where it is launched.
If we have launched a JavaScript program from "http://www.google.com", then it would be easy to access pages. To handle the same-origin policy, Selenium RC was introduced. In this, the server acts as a client-configured HTTP proxy and tricks the browser into believing Selenium Core and the Web application tested are from the same origin.
Locator in Selenium is a command that tells Selenium IDE about which GUI elements need to operate. There are various types of locators available in Selenium WebDriver, and its choice depends largely on the application under test.
Locating by ID: It takes a string parameter which is an ID attribute value and returns the object to the findElement() method.
driver.findElement(By.id("user"));
Locating by TagName: It locates all elements with a matching tag name.
driver.findElement(By.tagName("button").click());
Locating by Link: The target link can be located using a by.link text locator.
driver.findElement(By.linkText("Today's deals")).click();
Locating by Name: The first element with the name attribute value will return the location matched.
driver.findElement(By.name("books").click());
Locating by XPath: It takes a string parameter which is XPathExpression and returns it to an object to findElement() method.
driver.findElement(By.xpath("//span[contains(text(),'an account')]")).getText();
Locating by ClassName: It finds elements based on class attribute value.
driver.findElement(By.className("inputtext"));
Locating by CSS selector: It locates elements based on the drivers underlying the CSS selector engine.
driver.findElement(By.cssSelector("input#email")).sendKeys("myemail@email.com");
Visit here to learn Selenium Training in Hyderabad
Waits are commands in Selenium that are important for executing test scripts.
There are three types of waits in Selenium, such as:
Implicit Wait Type: Implicit wait commands direct Selenium WebDriver to wait for a certain measure of time before throwing a "no such element" exception.
Syntax:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Syntax:
WebDriverWait wait = new WebDriverWait(WebDriver Reference,TimeOut);
Syntax:
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);
Navigate To Command: This method loads a new web page in the current browser window and returns a string as a parameter.
driver.navigate().to(appUrl);
driver.navigate().forward();
Back Command: This method does the same operation like the back button of any browser. Neither returns nor accepts anything.
driver.navigate().back();
Refresh Command: This method is used for refreshing the current page.
driver.navigate().refresh();
Page Object Model is a class that represents a web page and its functionality and members. Page Factory is a way of initializing web elements you want to interact with the page object while creating an instance of it.
The robot class in Selenium is used to handle mouse and keyboard functions. It closes the pop windows. Using the WindowHandle() function, we can handle the pop-up window. By using third-party tools also we can handle pop-ups and other window-based features.
[ Check out Top Selenium WebDriver Interview Questions and Answers ]
switchTo() Command is used to switch the focus to a new window browser, by supplying the Window Handle or Window Name as an argument to the Command.
driver.SwitchTo().Window(WindowHandle);
driver.SwitchTo().Window(WindowName);
To get the current Window Handle, use the following command:
String currentWindowHandle = driver.CurrentWindowHandle;
findElement() is used for accessing a single web element on a page. It returns the order of the first matching element of the specified order. It throws a NoSuchElementException when it fails to send the element.
Syntax:
driver.findElement(By.xpath("Value of Xpath"));
Syntax:
List link = driver.findElements(By.xpath("Value of Xpath"));
/html/body/div[2]/div[1]/div[1]/a
//div[class="qa-logo"]/a
The following syntax is used to launch a browser using Selenium WebDriver:
To verify the visibility of web elements like checkbox, edit box, radio button, etc., use the following methods:
Syntax:
Boolean result = driver.findElement(By.xpath("//span[text()='Coding Ground']")).isDispayed();
Syntax:
Boolean btnresult = driver.findElement(By.xpath("//xpath[contains(@class,'gsc-search-button')]")).isSelected();
Syntax:
Boolean btnresult = driver.findElement(By.xpath("//xpath[contains(@class,'gsc-search-button')]")).isEnabled();
Visit here to learn Selenium Training in Bangalore
The value in the dropdown can be selected using WebDriver's Select class.
Syntax:
select By VisibleText:
Select selectByVisibleText = new Select (driver.findElement(By.id("SelectID_Two")));
selectByVisibleText.selectByVisibleText("Lime");
select By Value:
Select selectByValue = new Select(driver.findElement(By.id("SelectID_One")));
selectByValue.selectByValue("greenvalue");
select By Index:
Select selectByIndex = new Select(driver.findElement(By.id("SelectID_Three")));
selectByIndex.selectByIndex(2);
The iFrame is an inline frame or web page used for inserting another document within the current HTML document. It is often used to add content from other sources like an advertisement on a web page. It is defined with the "iframe" tag.
Select iframe by id
driver.switchTo().frame("ID of the frame");
driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));
frame(index)
driver.switchTo().frame(0);
frame(WebElement element)
driver. quit() is used for exiting the browser, tabs, pop-ups, session, etc., whereas the driver.close() is used to close the web browser window that the user is currently working on.
WebDriver offers a wide range of utilities that users can exploit to automate mouse and keyboard events. Action interface is one such utility that simulates single user interactions.
Here are the methods Actions class has provided for Mouse Hover action:
Junit annotations in Selenium are used for identifying method types defined in test code. To execute Selenium WebDriver testing with JUnit, it's necessary to add Junit annotation in the script.
Commonly used JUnit Annotations in Selenium are listed below:
No, captcha and barcode readers cannot be automated. Captcha functionality is to ensure that automated programs and bots don't get access to sensitive information - which is why Selenium cannot automate it.
We can minimize and maximize the browsers while we are testing an application in Selenium.
maximize() method is used for maximizing the browser, and minimize() method is used for minimizing the browser.
For example,
from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Firefox(executable_path="C:geckodriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.mindmajix.com/index.htm")
#to refresh the browser
driver.refresh()
# to minimize the browser window
driver.minimize_window()
#to close the browser
driver.close()
Explore Selenium Sample Resumes! Download & Edit, Get Noticed by Top Employers!
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Selenium Training | Dec 21 to Jan 05 | View Details |
Selenium Training | Dec 24 to Jan 08 | View Details |
Selenium Training | Dec 28 to Jan 12 | View Details |
Selenium Training | Dec 31 to Jan 15 | View Details |
Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .