Selenium interview questions


Are you wondering how to prepare for your next Selenium interview? Well, here is a list of typical Selenium interview questions that will help prepare you for that.

#1

Which one of the following is not a method for locating web elements? (Choose all that apply)

A. id()
B. linkText()
C. xpath()
D. class()
E. cssSelector()
F. tagName()
G. None of the above

Answer

D

There are eight strategies to locate web elements using Selenium and they are as follows.

LocatorMethod (Java)Description
class nameclassName()Locates elements whose class name contains the search value (compound class names are not permitted)
css selectorcssSelector()Locates elements matching a CSS selector
idid()Locates elements whose ID attribute matches the search value
namename()Locates elements whose NAME attribute matches the search value
link textlinkText()Locates anchor elements whose visible text matches the search value
partial link textpartialLinkText()Locates anchor elements whose visible text contains the search value. If multiple elements are matching, only the first one will be selected.
tag nametagName()Locates elements whose tag name matches the search value
xpathxpath()Locates elements matching an XPath expression

Click here to learn more about Selenium locators.

#2

When using the Java WebDriver library, which of the following is the correct way to visit a website? (Choose all that apply)

A. driver.getUrl("https://automatenow.io/");
B. driver.goToUrl("https://automatenow.io/");
C. driver.get("https://automatenow.io/");
D. driver.navigate().to("https://automatenow.io/");
E. driver.fetchUrl("https://automatenow.io/");
F. All of the above
Answer

C, D

The short way of visiting a website is to use driver.get() and the longer way is to use driver.navigate().to(). By using driver.navigate() we can do other things such as navigate back driver.navigate().back(), navigate forward driver.navigate().forward() and refresh the page driver.navigate().refresh().

#3

Which of the following commands notifies Selenium Grid that the browser is no longer in use so it can be used by another session? (Choose all that apply)

A. driver.endSession();
B. driver.terminateSession();
C. driver.quit();
D. driver.close(0);
E. driver.close();
Answer

C

Options A, B and D are incorrect given that they are not valid Selenium methods. Option E is only used to close a tab or window, but not the browser session. Per Selenium’s official website, calling driver.quit() does the following.

  • Closes all the windows and tabs associated with that WebDriver session.
  • Closes the browser process.
  • Closes the background driver process.
  • Notifies Selenium Grid that the browser is no longer in use so it can be used by another session (if you are using Selenium Grid).

Moreover, failure to call quit() will leave extra background processes and ports running on your machine which could cause you problems later.

#4

In Selenium 4, which command opens and switches to a new browser window?

A. driver.manage().window().new();
B. driver.manage().window().newWindow();
C. driver.switchTo().newWindow();
D. driver.switchTo().newWindow(WindowType.WINDOW);
E. None of the above
Answer

D

The command to open a new window in Selenium 4 is driver.switchTo().newWindow(WindowType.WINDOW). Click here to learn more.

#5

Which one of the following is a Relative Locator in Selenium 4? (Choose all that apply)

A. above
B. below
C. leftOf
D. rightOf
E. near
F. All of the above
Answer

A, B, E

Selenium 4’s Relative Locators include the following.

  • above
  • below
  • toLeftOf
  • toRightOf
  • near

Click here to learn more about Relative Locators.

#6

True or false, Selenium can be used to automate CAPTCHA.

Answer

False

CAPTCHA stands for the Completely Automated Public Turing test to tell Computers and Humans Apart. As its name implies, it is designed to prevent automation. As a result, Selenium, or any other automation tool, cannot be used to automate CAPTCHA.

#7

Which one of the following is a method in the ExpectedConditions class? (Choose all that apply)

A. alertIsPresent()
B. elementToBeClickable()
C. textToBe​()
D. titleContains
E. and()
F. All of the above
Answer

F

The expectedConditions class contains a set of predefined conditions that are used for frequent wait operations. Click here to learn more about this essential class.

#8

What’s the difference between methods findElement() and findElements() in Selenium?

Answer

Both of these methods require a parameter of type By which points to the element(s) that you wish to find. The main difference between the two is that the findElement() method is used to locate a single WebElement that matches a given locator while findElements() is used to locate multiple WebElements that match a given locator. Moreover, in the event that no element is found, findElement() throws a NoSuchElementException exception while findElements() simply returns an empty List.

Lastly, the findElements() method is multi use. For instance, it can be used to know how many items appear in a list or to make sure that a given element is not displayed.

#9

What is WebElement in Selenium?

Answer

WebElement refers to an interface in Selenium. This interface contains many of the common methods that one would use on an element such as getText(). It is also the fundamental object type that is returned by the findElement(By) method; a WebElement object that represents a particular DOM node such as a hyperlink or an input field.

Click here if you would like to learn more.

#10

Aside from getText(), name some of the other methods that can be called on a WebElement object?

Answer

Below is a list of all the methods that can be called on a WebElement object.

Method NameDescription
click()Click the element.
submit()If this current element is a form, or an element within a form, then this will be submitted to the remote server.
sendKeys()Use this method to simulate typing into an element, which may set its value.
clear()If this element is a form entry element, this will reset its value.
getTagName()Get the tag name of this element. Not the value of the name attribute: will return “input” for the element <input name=”xyz” />.
getDomProperty()Get the value of the given property of the element.
getDomAttribute()Get the value of the given attribute of the element. This method, unlike getAttribute(), returns the value of the attribute with the given name but not the property with the same name.
getAttribute()Get the value of the given attribute of the element. More exactly, this method will return the value of the property with the given name, if it exists. If it does not, then the value of the attribute with the given name is returned. If neither exists, null is returned.
getAriaRole()Gets result of computing the WAI-ARIA role of element.
getAccessibleName()Gets result of a Accessible Name and Description Computation for the Accessible Name of the element.
isSelected()Determine whether or not this element is selected or not. This operation only applies to input elements such as checkboxes, options in a select and radio buttons.
isEnabled()Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
getText()Get the visible (i.e. not hidden by CSS) text of this element, including sub-elements.
findElements()Find all elements within the current context using the given mechanism.
findElement()Find the first WebElement using the given method.
isDisplayed()Is this element displayed or not? This method avoids the problem of having to parse an element’s “style” attribute.
getLocation()Returns a point, containing the location of the top left-hand corner of the rendered element.
getSize()Gets the width and height of the rendered element.
getRect()Gets the location and size of the rendered element.
getCssValue()Get the value of a given CSS property. Color values should be returned as rgba strings, so, for example if the “background-color” property is set as “green” in the HTML source, the returned value will be “rgba(0, 255, 0, 1)”.

#11

What is JavascriptExecutor in Selenium?

Answer

The JavascriptExecutor is an interface in Selenium. As its name implies, it allows us to run JS code right from our Selenium tests. You can learn more about it by clicking here.

#12

True or false. Selectors linkText and partialLinkText call down to XPath selectors internally in WebDriver.

Answer

True

Click here to learn more.

#13

What is an explicit wait?

Answer

An explicit wait is used to halt program execution until a given condition is met (e.g., an alert to be present, an element or text to be visible, etc.). As a tester, you control how long to wait for and Selenium handles the rest. Essentially, Selenium will poll the DOM every 500 milliseconds for the condition that you specify and for as long as you specify.

Note that this type of wait ignores NotFoundException and that it throws a timeout exception if the condition is not met within the specified time.

Watch this video if you would like a detailed explanation of an explicit wait.

#14

What is an implicit wait?

Answer

An implicit wait specifies the amount of time the driver should wait when searching for any element if it is not immediately present. Note that it throws NoSuchElementException if no element is found.

Click here to learn more about this type of wait.

#15

What is a FluentWait?

Answer

A FluentWait allows us to define not only the maximum amount of time to wait for a given condition, but we are also able to specify the frequency with which to check for the condition. We may also ignore certain types of exceptions such as NoSuchElementException when searching for an element on the page.

Click here for an in depth look at FluentWait.

#16

What is the Actions class for?

Answer

Actions is a user-facing API for emulating complex user gestures. Here are some of the things that we can do with it.

  • Perform a right-click
  • Perform a double-click
  • Perform drag-n-drop operation
  • Send keys to the active element
  • Hover over an element

Watch these videos to see it in action (pun intended). 😬
https://youtu.be/rxkVioee6ig
https://youtu.be/pAk4Ht929Ug

#17

What is the Page Object Model (POM)?

Answer

The Page Object Model is a popular design pattern in software automated testing.

“A page object is an object-oriented class that serves as an interface to a page of your AUT (Application Under Test).”

https://bit.ly/3dIQNc3

Some of the benefits to using POM are listed below.

  • Improved test maintenance
  • Less code duplication

Check out this video if you would like to see an example.

#18

How does one get the title of the current page?

Answer

By using driver.getTitle().

The getTitle() method returns the title of the current page, with leading and trailing whitespace stripped, or null if one is not already set.

Click here to learn more.

#19

How does one make an assertion?

Answer

By using testing frameworks such as JUnit and TestNG.

Click here to learn more.

#20

How is text retrieved from an element?

Answer

By using the getText() method as shown below.

driver.findElement(By.id("element_id")).getText();

This method gets the visible (i.e., not hidden by CSS) text of a given element.

Click here to learn more.

#21

How is text entered?

Answer

The most common way of doing this is by using the sendKeys() method as shown below.

driver.findElement(element_locator).sendKeys("Hello");

It is also possible to set the text by using the JavascriptExecutor interface.

Click here to learn more.

#22

How is check box selected?

Answer

To check a check box, we simply click on it. To uncheck it, we click on it again.

Click here to learn more.

#23

How is a dropdown menu option selected?

Answer

Selenium offers a special class called Select to work with dropdown menus. This class offers three methods to select from a dropdown.

  • selectByVisibleText()
  • selectByIndex()
  • selectByValue()

Click here to learn more.

#24

How is a radio button selected?

Answer

By finding the radio button that we wish to select and clicking on it. Moreover, we can verify our selection by using the isSelected() method.

Click here to learn more.

#25

How is the browser’s back button clicked?

Answer

By using the following.

driver.navigate().back();

Click here to learn more.

#26

How does one switch between open windows?

Answer

By using the getWindowHandle() and getWindowHandles() methods.

Click here to learn more.

#27

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

Answer

The driver.close() closes the current window or quits the browser if it is the last window currently open.

The driver.quit() quits the driver and closes every associated window.

Click here to learn more.

#28

How is a new browser tab opened in Selenium?

Answer

If you’re using Selenium 4, you can use the following.

driver.switchTo().newWindow(WindowType.TAB);

If not, you can use the JavascriptExecutor interface as follows.

((JavascriptExecutor) driver).executeScript("window.open()");

Click here to learn more.

#29

How does one read data from a table?

Answer

The strategy used will depend on the type of table. Some tables may just be a few rows and columns while others may contain many rows and columns and even pagination.

To learn how to extract data from a simple table click here.

To learn how to extract data from a table that contains pagination click here.

#30

How is drag-and-drop operation performed?

Answer

We can use the Actions class to perform drag-and-drop operations. This class has two special methods and they are as follows.

  • dragAndDrop(WebElement source, WebElement target) – A convenience method that performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
  • dragAndDropBy(WebElement source, int xOffset, int yOffset) – A convenience method that performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.

Click here to learn more.

#31

How do you click an alert’s OK button?

Answer

By using the following command.

driver.switchTo().alert().accept();

Click here to learn more.

#32

What is the WebDriverWait class used for in Selenium?

Answer

It provides the ability to wait for an arbitrary condition during test execution. Moreover, it is commonly used in combination with the ExpectedConditions class to wait for things such as the following.

  • The page tile to be a given text.
  • The visibility of a given WebElement.
  • An alert to be present.
  • A WebElement to have a given text.

Click here to learn more.

#33

True or false. To enter text in a modal window, you most use the switchTo() method.

Answer

False.

Although a modal windows is similar to an alert pop-up, it is not required to use the switchTo() method before entering text there. Meanwhile, we must use that method if we wish to enter text in an alert pop-up.

Click here to learn more.

#34

How do you hover over an element?

Answer

It is done by using the Actions’ class moveToElement() method as follows.

WebElement element = driver.findElement(element_locator);
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();

Click here to learn more.

#35

How do you scroll an element into view?

Answer

This can be accomplished by using the JavascriptExecutor interface. A code snippet follows.

WebElement element = driver.findElement(element_locator);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].scrollIntoView();", element);

Click here for a detailed explanation of what each of the lines of code does.

#36

How do you scroll a web page up or down?

Answer

This can be accomplished by using Selenium’s JavaScriptExecutor interface along with the scrollBy() method from the HTML DOM’s Window object. See below example.

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("window.scrollBy("number_of_pixels_x-axis","number_of_pixels_y-axis");");

Click here to learn more.

#37

How do you select a submenu item?

Answer

One way to do this is by using the moveToElement() method from the Actions class to hover over menu options until you find the one you are looking for and clicking on it.

Click here to learn more.

#38

Let’s say that you are testing a web page with a single table that has n number of rows. How would you write a test that counts the number of table rows?

Answer

This can be done by counting the number of table row (tr) elements for the given table.

4 thoughts on “Selenium interview questions

Leave a Reply