Are you wondering how to prepare for an upcoming Selenium interview? Here is a list of typical Selenium interview questions that will help prepare you for that.
You will love our QA and Java interview questions if you enjoy this content.
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
D
There are eight strategies to locate web elements using Selenium, and they are as follows.
Locator | Method (Java) | Description |
---|---|---|
class name | className() | Locates elements whose class name contains the search value (compound class names are not permitted) |
css selector | cssSelector() | Locates elements matching a CSS selector |
id | id() | Locates elements whose ID attribute matches the search value |
name | name() | Locates elements whose NAME attribute matches the search value |
link text | linkText() | Locates anchor elements whose visible text matches the search value |
partial link text | partialLinkText() | Locates anchor elements whose visible text contains the search value. If multiple elements are matching, it will select only the first one. |
tag name | tagName() | Locates elements whose tag name matches the search value |
xpath | xpath() | Locates elements matching an XPath expression |
Click here to learn more about Selenium locators.
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
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().
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();
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.
- It closes all the windows and tabs associated with that WebDriver session.
- It closes the browser process.
- It closes the background driver process.
- Notifies Selenium Grid that the browser is no longer in use so that 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.
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
D
The command to open a new window in Selenium 4 is driver.switchTo().newWindow(WindowType.WINDOW). Click here to learn more.
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
A, B, E
Selenium 4’s Relative Locators include the following.
- above
- below
- toLeftOf
- toRightOf
- near
Click here to learn more about Relative Locators.
True or false, one can use Selenium to automate CAPTCHA.
False
CAPTCHA stands for the Completely Automated Public Turing test to tell Computers and Humans Apart. As its name implies, its purpose is to prevent automation. As a result, Selenium, or any other automation tool, cannot be used to automate CAPTCHA.
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
F
The expectedConditions class contains a set of predefined conditions used for routine wait operations. Click here to learn more about this essential class.
What’s the difference between methods findElement() and findElements() in Selenium?
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. In contrast, 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, we can use it to know how many items appear in a list or ensure that a given element is not displayed.
What is WebElement in Selenium?
WebElement refers to an interface in Selenium. This interface contains many standard methods that one would use on an element, such as getText(). Also, it is the fundamental object type 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.
Aside from getText(), name some of the other methods we can call on a WebElement object?
Below is a list of all the methods we can call on a WebElement object.
Method Name | Description |
click() | Click the element. |
submit() | If this current element is a form, or an element within a form, it will submit the same 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. Unlike getAttribute(), this method 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 attribute’s value with the given name is returned. It returns null if neither exists. |
getAriaRole() | Gets result of computing the WAI-ARIA role of an element. |
getAccessibleName() | Gets result of an 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 select and radio buttons. |
isEnabled() | Is the element currently enabled or not? That 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)”. |
What is JavascriptExecutor in Selenium?
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.
True or false. Selectors linkText and partialLinkText call down to XPath selectors internally in WebDriver.
True
Click here to learn more.
What is an explicit wait?
It is an explicit wait used to halt program execution until meeting a given condition (e.g., an alert to be present, an element or text to be visible, etc.). Selenium will essentially poll the DOM every 500 milliseconds for the condition you select and for as long as you decide. As a tester, you control how long to wait for, and Selenium handles the rest.
Note that this type of wait ignores NotFoundException and throws a timeout exception if not meeting the condition within the specified time.
Watch this video if you would like a detailed explanation of an explicit wait.
What is an implicit wait?
An implicit wait specifies the time the driver should wait when searching for any element if it is not immediately present. Note that if we find no element, it throws NoSuchElementException.
Click here to learn more about this type of wait.
What is a FluentWait?
A FluentWait allows us to define the maximum time to wait for a given state, but we can also specify the frequency with which to check for the condition. We may also ignore certain exceptions, such as NoSuchElementException, when searching for an element on the page.
Click here for an in-depth look at FluentWait.
What is the use of the Actions class?
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
What is the Page Object Model (POM)?
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).”
Source
Some of the benefits of using POM are listed below.
- Improved test maintenance
- Less code duplication
Check out this video if you would like to see an example.
How does one get the title of the current page?
By using driver.getTitle().
The getTitle() method returns the current page’s title, with leading and trailing whitespace stripped or null if one is not already set.
Click here to learn more.
How does one make an assertion?
We perform assertions by using testing frameworks such as JUnit and TestNG.
Click here to learn more.
How is text retrieved from an element?
By using the getText() method as shown below.
driver.findElement(By.id("element_id")).getText();
This method gets a given element’s visible (i.e., not hidden by CSS) text.
Click here to learn more.
How is text entered?
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.
How is a check box selected?
To check a check box, we simply click on its WebElement. To uncheck it, we click on it again.
Click here to learn more.
How is a dropdown menu option selected?
Selenium offers a particular class called Select to work with dropdown menus. This class provides three methods to select from a dropdown.
- selectByVisibleText()
- selectByIndex()
- selectByValue()
Click here to learn more.
How is a radio button selected?
First, we find the button we wish to choose, then click on it. Moreover, we can verify our selection by using the isSelected() method.
Click here to learn more.
How is the browser’s back button clicked?
By using the following.
driver.navigate().back();
Click here to learn more.
How does one switch between open windows?
By using the getWindowHandle() and getWindowHandles() methods.
Click here to learn more.
What is the difference between driver.close() and driver.quit()?
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.
How is a new browser tab opened in Selenium?
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.
How does one read data from a table?
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.
How is a drag-and-drop operation performed?
We can use the Actions class to perform drag-and-drop operations. This class has two unique 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.
How do you click an alert’s OK button?
By using the following command.
driver.switchTo().alert().accept();
Click here to learn more.
What is the WebDriverWait class used for in Selenium?
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 title is to be a given text.
- The visibility of a given WebElement.
- An alert is to be present.
- A WebElement to have a given text.
Click here to learn more.
True or false. To enter text in a modal window, you must use the switchTo() method.
False.
Although a modal window is similar to an alert pop-up, it is not required to use the switchTo() method before entering text. Meanwhile, we must use that method if we wish to enter text in an alert pop-up.
Click here to learn more.
How do you hover over an element?
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.
How do you scroll an element into view?
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.
How do you scroll a web page up or down?
Using Selenium’s JavaScriptExecutor interface and the scrollBy() method from the HTML DOM’s Window object, we can accomplish it. See the 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.
How do you select a submenu item?
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 click on it.
Click here to learn more.
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?
You can count the number of table row (tr) elements for the given table.
Follow our blog
Be the first to know when we publish new content.
Very useful thank you!
You’re very welcome Tetiana 🙂.
Thanks, this is informative!!
You’re very welcome Albin!