Home / Selenium

Selenium Interview Questions

Rating: 5.0Blog-star
Views: 109410
by Madhuri Yerukala
Last modified: July 16th 2021

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.

Types of Selenium Interview Questions

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.

Top Frequently Asked Selenium Interview Questions

  1. What is Selenese? And explain its types?
  2. What are the limitations of Selenium?
  3. What are locators in Selenium?
  4. What is the latest version of Selenium?
  5. What's new in Selenium 4.0?
  6. How can you click on a hyperlink in Selenium?
  7. What are Junit annotations?
  8. How to handle pop-ups in Selenium?

Basic Selenium Interview Questions

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.

[ Related Article: What is Selenium? ]

Mainly selenium commands are of three types:

  1. Actions: Manipulates web application state.
  2. Accessors: Checks the web application state and sorts the results in some variables.
  3. Assertions: Verifies application state corresponding to an expected state specified by the user.

2. What are the limitations of Selenium?

  • Doesn't supports desktop applications testing
  • Unavailability of reliable tech support, as Selenium requires a high level of expertise and resources to manage.
  • Since Selenium is open-source software, we need to rely on community forums to get technical issues resolved.
  • Doesn’t support automation tests on REST and SOAP platforms.
  • Limited support for image testing
  • No built-in reposting and test management feature
  • Need to rely on third-party tools for testing mobile and desktop applications.

 [ Related Article: Selenium Career Opportunities ]

3. List the testing types supported by Selenium?

Types of testing supported by Selenium are as follows:

  • Acceptance testing: Determines the feature or system that meets customer expectations and requirements.
  • Functional testing: Determines if a feature or system functions well without issues.
  • Performance testing: They are performed to measure how well an application is performing
  • Regression testing: Generally, this test is done after a fix, change or feature is added. 
  • Test-driven development (TDD): It's an iterative development methodology in which test drives the feature design.
  • Behavior-driven development (BDD): It's also an iterative development methodology, in which the objective is to include all the parties in the development of an application. 

4. What is the latest version of Selenium?

Selenium's latest version is Selenium 4.0.0 Alpha 5, which is released in March 2020.

Selenium 4.0 comes with new standardization and offers a seamless experience. 

The key features of Selenium 4.0 Alpha 5 are:

  • Improved docker support using domain sockets and the DOCKER_HOST env variable
  • Replaces OpenTracing with OpenTelemetry.
  • Instead of creating a client per session, reuses the same HTTP client.
If you want to become a Selenium Certified Specialist, then visit Mindmajix - A Global online training platform: “Selenium Training”.  This course will help you to achieve excellence in this domain.

5. What's new in Selenium 4.0?

  • W3C WebDriver Standardization
  • Selenium 4 IDE TNG
  • Refreshed Documentation
  • Improved Selenium Grid
  • Better Window and Tab management
  • Relative locators

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();

7. How to add text in the text box without using sendkeys()?

Using JavaScriptExecutor, we can enter text in the text box.

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('email').value="123.ab@xyz.com");

8. What is StaleElementReferenceException, and how do you handle it?

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.

9. What is the same-origin policy? And how can you avoid it?

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 rescues from attack vectors.

For example, for a URL http:// 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 Web application tested are from the same origin.

10. What are locators in Selenium? 

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.

[ Related Article: Learn Selenium IDE ]

  • 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");

Advanced Selenium Interview Questions

11. How many types of waits are there in Selenium?

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);

  • Explicit Wait Type: Explicit wait command directs WebDriver to wait until a certain condition occurs before proceeding with executing the code.

Syntax:

WebDriverWait wait = new WebDriverWait(WebDriver Reference,TimeOut);
  • Fluent Wait Type: Fluent wait command defines Selenium WebDriver to wait for a specific condition to appear. And, also determines the frequency with which Selenium Webdriver checks the state that appeared before by throwing "ElementNotVisibleException".

Syntax: 

Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);

12. What are the most commonly used Browser navigation commands for Selenium WebDriver?

  • 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);
  • Forward Command: This method does the same operation as the forward button of any browser. It neither returns nor accepts anything
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();

[ Related Article: Selenium Online Tutorial ]

13. How is the Page Object Model(POM) different from Page Factory?

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.

14. How to handle pop-ups in Selenium?

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.

15. What is the use of switchTo() command in Selenium?

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;

16. How is findElement() different from findElements()?

  • 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"));
  • findElements() returns the list of all the matching elements. findElements method returns an empty list when an +element is not available or doesn't exist on the page.

Syntax:

List link = driver.findElements(By.xpath("Value of Xpath"));

17. What is the major difference between "/" and "//" in Xpath?

  • Single Slash “/”: It is used to create Xpath with an absolute path that is Xpath would be created to start selection from document node/start node.
/html/body/div[2]/div[1]/div[1]/a
  • Double Slash "//": It is used to create Xpath with a relative path that is Xpath would be created to create start selection from anywhere within the document

//div[class="qa-logo"]/a

18. How can you launch a browser using Selenium WebDriver?

The following syntax is used to launch a browser using Selenium WebDriver:

  • WebDriver driver = new InternetExplorerDriver();
  • WebDriver driver = new ChromeDriver();
  • WebDriver driver = new FirefoxDriver();

19. How to verify whether an element is displayed or not on screen in Selenium?

To verify the visibility of web elements like checkbox, edit box, radio button, etc., use the following methods:

  • isdisplayed(): Checks if a web element is present on the screen.

Syntax: 

Boolean result = driver.findElement(By.xpath("//span[text()='Coding Ground']")).isDispayed();
  • isSelected(): Checks the status of the check box, radio button, and options in the static dropdown.

Syntax:

Boolean btnresult = driver.findElement(By.xpath("//xpath[contains(@class,'gsc-search-button')]")).isSelected();
  • isEnabled(): Checks if an element is enabled or not.

Syntax:

Boolean btnresult = driver.findElement(By.xpath("//xpath[contains(@class,'gsc-search-button')]")).isEnabled();

20. How to select a value from DropDown using Selenium Webdriver?

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);

21. How to handle frames using WebDriver?

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");

  • Locating iframe using tagName

driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));

  • Locating iframe using index

frame(index)
driver.switchTo().frame(0);
  • Switch to Frame by WebElement

frame(WebElement element)

22. What is the difference between driver.quit() command and driver.close()?

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.

23. How to perform mouse hover functions on a web element using WebDriver?

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:

  • moveToElement(WebElement target)
  • moveToElement(WebElement target, int xOffset, int yOffset)

24. What are Junit annotations?

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:

  • @BeforeClass
  • @Before 
  • @Test 
  • @After 
  • @AfterClass 
  • @Ignore 

25. Can Captcha be automated?

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. 

26. How to minimize and maximize browsers in Selenium with python?

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()

About Author

author
NameMadhuri Yerukala
Author Bio

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 .