Selenium Q A
Selenium Q A
Aspirants looking to excel in Selenium can make use of these top 110+ Selenium
interview questions and answers. Whether you are a fresher or an experienced
professional, you can test your knowledge of Selenium, and your ability to think critically
and solve problems with these questions and answers.
1. What is Selenium?
Selenium is a free and open source web automation testing tool. It can be used to
perform actions like click, type, mouse and keyboard actions etc on the elements on the
web page and validate the outcomes generated after performing them.
Selenium supports multiple languages namely Java, C#, Python, JavaScript etc and can
be used in various platforms like Windows, Mac, iPhone, etc. It comprises of a group of
tools listed below −
Selenium IDE
Selenium Webdriver
Selenium Grid
Selenium is a suite of tools consisting of multiple tools, each of them used to perform
India to Dubai
various items in web based testing. The Selenium components consist of −
from India
from Rs16,850
Page 2 of 44
Selenium Integrated Development (IDE) − This tool is primarily used for web
based testing and popular for the record and playback feature.
Selenium Remote Control (RC) − This tool is just like a server and is capable
of generating scripts in multiple languages. It takes commands from the tests and
directs them to the browser in the form of Selenium core JavaScript commands.
Selenium Webdriver − This tool can be used to design test scripts in multiple
languages by directly interacting with web browsers
Selenium Grid − This tool is used for enabling parallel execution of test scripts
on multiple devices, browsers, operating systems etc.
It can be used with multiple browsers like Chrome, Firefox, Safari, Edge etc.
It can be used with multiple programming languages like the Java, JavaScript,
C#, Ruby, Python, Kotlin etc.
It follows a user-friendly interface which helps the user to create and maintain
test scripts.
It is not a single tool but a collection of tools each of which can be used for
different testing purposes.
It needs some time prior to being used comfortably with newer versions of
browsers and platforms.
Automation testing is the process of converting the manual test cases to automation with
the help of testing tools. The manual tests which have a large number of repeated and
redundant steps are mostly chosen as the right candidates for automation. However, it
must be noted that it is impossible to automate 100% of the manual test cases. Also,
automation can be adopted only on a stable software.
It can be used for verifying both the functional and non-functional requirements
of the software.
It has a huge return on investment as it saves a lot of time, money, and human
effort.
Parallel execution can be achieved through automation.
The time saved through automation can be used to manually test very complex
scenarios which can not be automated.
It lets the testers execute regression and repeated test cases multiple times in a
test cycle.
Selenese are a group of commands which are used to perform web based testing. These
commands are widely used in Selenium IDE. Selenese Commands are of three types,
namely Actions, Accessors, and Assertions.
Page 4 of 44
The Selenium 3.x extends the Remote WebDriver while the Selenium 4.x extends the
Chrome Driver. Besides, the 3.x version needs triggering of Hub and Node jars to run
Selenium Grid tests, which is no longer required in the 4.x version. Selenium 4.x version
provides Selenium IDE Plugins for Chrome and Firefox. It also comes with the side
runner in the latest version of Selenium.
What are the differences between Selenium 2 and Selenium 3? Answer: Selenium 3.0 is
an improved version of Selenium 2.0 and it has come up with bug fixes of Selenium 2.0
version. Selenium 2.0 was the combination of Selenium RC and Selenium Webdriver.
However, Selenium RC became obsolete from the Selenium 3.0 version.
Selenium 2.0 did not have much scope for mobile testing however from Selenium 3.0
mobile testing is carried out using Appium and it has more updated and improved APIs
than Selenium 2.0. Selenium 2.0 is no longer in use now.
Some of the tools used for automation testing are listed below −
Cypress − It is mainly used for web based and API automation testing.
Selenium identifies elements on the web page with the help of its locating strategies.
Some of the locators available in Selenium are listed below −
Page 5 of 44
Css
Xpath
Link Text
Id
Name
Class Name
Tagname
From the latest version of Selenium, there are additional locators available called the
relative locators which identify elements with respect to the position of another element.
Some of Relative Locators in Selenium are listed below −
above()
below()
toRightOf()
toLeftOf()
near()
Chaining of locators
12. What are the different types of testing that can be done using
Selenium?
The different types of testing that can be performed with Selenium are listed below −
XML is an extensible markup language used to store, manage, and send data. The data
is stored in a key-value pair in a similar format as HTML. Xpath are of two types: relative
xpath and absolute xpath.
The absolute xpath is denoted by the double slash or //. It identifies the desired element
starting right from the document’s beginning node. But, if there is a change in path to
any element(in between from the root to the current node), the existing absolute xpath
will stop working to identify that particular element.
Example
The relative xpath is denoted by the single slash or /. It identifies the desired element
starting from anywhere in the DOM.
Example
The driver.quit() is used to close all the opened windows and tabs and terminate the
driver session. If the instance of the driver is again invoked after driver.quit(), an
exception will be thrown on session id as null.
16. How to wait until a page has been completely loaded using
Selenium?
We can wait until a page has been completely loaded using Selenium using the below
methods −
Page 7 of 44
Using the Implicit Wait − In this approach, on every page load it shall wait until the
page is completely loaded.
For Example −
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
For Example −
if (document.readyState == "complete"){
alert("Page is loaded completely");
}
An exception test is the one which anticipates an exception that will be received as an
output from a test class. It looks for the @Test annotation then the name of the expected
exception within the brackets.
For Example −
@Test
public void test1(expectedException = NoSuchElementException.class){
The above exception test for elements which can not be identified.
The various annotations which are used along with the Selenium are usually a part of the
TestNG test automation framework. Some of annotations used along with the Selenium
are listed below −
@BeforeSuite
@AfterSuite
@BeforeClass
@AfterClass
@BeforeSuite
@AfterSuite
@BeforeTest
@AfterTest
Thus a web browser enables scripts from a particular web page to approach another web
page if both of them are from a similar origin. The same-origin policy protects a web
page’s secured data from the malicious attack from another web page from a different
origin.
To avoid the same-origin policy, the proxy injection technique is adopted. In this method,
the Selenium server conducts as a client configured HTTP proxy and tricks the browser
into accepting that the Selenium Core and the application under test belong to the same
origin.
UFT is a licensed tool and it comes with a cost, whereas Selenium is free and
open source.
Page 9 of 44
UFT is used to test only the windows applications, whereas the Selenium can test
on multiple platforms like Windows, Linus, Mac, iPhone, Blueberry etc.
UFT is only supported with Visual Basic scripting, whereas the Selenium works
with multiple programming languages like Java, C#, Python, JavaScript, Ruby,
Kotlin etc.
If the testing team is looking for a free, open source tool and is well equipped
with the programming languages, then Selenium is always the better choice than
UFT. However, if the testing team is only aware of VB scripting and requirement is
to verify in the Windows platform, then UFT can be chosen.
23. What is POM or Page Object Model and what are its benefits?
POM also known as Page Object Model is a design pattern that can be used to create
Selenium test scripts by separating the elements and test case flows into individual class
files. The benefits of POM are that by separating the web elements and the test cases, it
gives more code readability, reusability, and easy maintenance.
Implicit Wait
It is of default type where the Selenium waits for a specific amount of time prior to
throwing an exception.
For Example −
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Explicit Wait
This wait is used to inform the Webdriver to wait for a specific condition for the given
amount of time prior to throwing an exception.
For Example −
Fluent Wait
This wait is used to inform the Webdriver to wait for a specific condition at a given
interval during which the condition is checked for a particular amount of time prior to
throwing an exception.
For Example −
wt.until(ExpectedConditions.titleIs("Online Tutorialspoint"));
Selenium Webdriver is a part of the suite of tools within the Selenium available from the
Selenium version 2. It can be used to automate tests on web applications using multiple
languages, platforms, and browsers.
Selenium Webdriver is a free and open source library used to create automation test
scripts for web based applications.
The different types of navigation commands used in Selenium are listed below −
Page 11 of 44
driver.navigate()
.to("https://www.tutorialspoint.com/selenium/practice/login.php");
driver.get("https://www.tutorialspoint.com/selenium/practice/login.php"
);
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
The driver.get() method launches a URL and waits for the complete page to load,
whereas the driver.naviaget.to() method launches a URL without waiting for the
complete page to load.
The driver.get() method does not allow the browser to move forward or backward
to the browser history and its refresh, while the driver.naviaget.to() method
allows all these.
The method driver.get() launches the browser, opens the application URL these, and
waits for the complete page to load.
Syntax
Page 13 of 44
driver.get("https://www.tutorialspoint.com/selenium/index.htm");
To get more information on the driver.get(), please visit the link Selenium WebDriver
Browser Navigation
Syntax
driver.navigate()
.to("https://www.tutorialspoint.com/selenium/index.htm");
Syntax
// identify element
WebElement m = driver.findElement(By.xpath("value of xpath locator"));
The getWindowHandles() method returns all the handle ids of the windows and
tabs currently opened, whereas the getWindowHandle() method returns the
handle id of the window or tab which is in focus.
The getWindowHandles() method returns all the handle ids of the windows and
tabs currently opened using the Collection Set, while the getWindowHandle()
method returns the handle id of the Window or Tab which is in focus as a String.
The same can also be done with the statement FireFoxDriver driver = new
FireFoxDriver(), however to optimize the code we mostly use WebDriver driver = new
FireFoxDriver(). Also, this statement will only have the option to use, and invoke
methods of the FireFoxDriver which is applicable for Firefox browser.
We cannot create objects of a specific driver, say FirefoxDriver and assign it to another
browser driver, say ChromeDriver. So the statement ChromeDriver driver = new
FireFoxDriver() is wrong. But we can assign the objects of FirefoxDriver, ChromeDriver,
etc to the WebDriver interface. The statement WebDriver driver = new WebDriver() is not
used as WebDriver is an interface and we cannot create objects of an interface.
As per the hierarchy of the classes and interfaces, the SearchContext is the WebDriver’s
super interface. The hierarchy of the classes and interfaces in Selenium is described in
the below diagram −
Using Actions
//div/a[5]
The above xpath identifies the fifth anchor child for the parent div.
Disable all the breakpoints by pressing Disable breakpoints. The breakpoints get
enabled on pressing the same button again.
We can handle Alerts and Popups in Selenium using the Alerts interface. Initially, the
driver focus is on the main web page, to access the alerts, we need to shift the driver
focus from the main page to them by the switchTo().alert() method.
50. What are the differences between assert and verify commands
in Selenium IDE?
The assert and verify commands are used to add verification points in the test case. If
the condition being checked in an assert command is not satisfied, the execution stops at
the point of mismatch. Else the execution is continued. If the condition being checked in
a verify command is not satisfied, the execution still continues after the point of
mismatch.
Syntax
To scroll down to the bottom of the page, again the scrollBy method is used.
Syntax
Syntax
WebElement t = driver.findElement
(By.xpath("<value of xpath>"));
Syntax
Syntax
Syntax
FindBy(id = "name")
WebElement elm;
Syntax
// Identify element
WebElement textBox = driver.findElement
(By.className("<value of class name>"));
communication of the Selenium client library to the browser drivers is done via the W3C
WebDriver protocol.
The web browsers and their drivers were already W3C compliant, so the Selenium
Webdriver adopted the same protocol so that a standard and common protocol is
followed in the entire architecture. It also brings stability to the tests and common code
can be used for multiple browsers. Besides the Actions API used in Selenium now
provides additional features like two key press, zoom in/out etc.
While working with Selenium we face numerous exceptions. An exception occurs when an
element is not found or if there is a mismatch between the expected and actual outcome,
or for any other reasons. The code generates an exception to end the test in a logical
way.
LoadRunner
JMeter
Silk Performer
Gatling
Apache
Neoload
The single slash or / refers to the absolute xpath and the double slash or // refers to the
relative xpath. The absolute path starts from the root node to locate an element while
the relative path starts from anywhere in the DOM to identify an element.
Example
The various types of Webdriver API used in Selenium are listed below −
ChromeDriver
FirefoxDriver
InternetExplorerDriver
HTMLUnitDriver
SafariDriver
iPhoneDriver
RemoteWebDriver
AndroidDriver
iPhoneSimulatorDriver
EventFiringWebDriver
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
driver.get("https://www.tutorialspoint.com/selenium/practice/radio-
button.php");
// Closing browser
driver.quit();
}
}
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
driver.get("https://www.tutorialspoint.com/selenium/practice/radio-
button.php");
// Closing browser
driver.close();
}
}
We can get the text of an element in Selenium by using the getText() method.
Syntax
// Identify element
WebElement textBox = driver.findElement
(By.className("<value of class name>"));
No we cannot test the mobile applications using the Selenium Webdriver. We test them
normally using the Appium which is built on the top of Selenium.
We can perform the drag and drop operations in Selenium with the help of the methods -
dragAndDrop() and dragAndDropBy() of the Actions class. The dragAndDrop method
has two parameters - source and destination locators. The dragAndDropBy method has
three parameters - source locator, x offset value in horizontal direction, and y offset
value in vertical direction.
We can handle cookies in Selenium. We can get, add, and delete cookies. The basic
methods on cookies are listed below −
We can handle multiple windows in Selenium. Initially, the driver focus is on the main
window, to access the other window, we need to shift the driver focus from the main
window to the other by using the switchTo().window("child window handle id") method.
getWindowHandles() it returns the handle ids of all the windows currently opened
using the Collection Set.
73. How to enter text in a text box without using the sendKeys()
method in Selenium?
We can enter text in a text box without using the sendKeys() method. This can also be
done with the JavaScript Executor.
Syntax
Syntax
// identify element
WebElement m1 = driver.findElement
(By.xpath("//div/div[3]/nav/div/a"));
Actions actions = new Actions(driver);
actions.moveToElement(m1).click().build().perform();
The Page Factory is a class which assists in implementing the Page Object Model. It is an
extension of the Page Object Model and is available from the WebDriver class.
We can select an option in the dropdown using the methods of the Select class. The
dropdowns in a web page are represented by select tagname and its options are
represented by the options tagname. There are multiple Select class methods in
Selenium Webdriver. They are listed below −
options = sel.getFirstSelectedOption();
isMultiple() − Returns a true value if the dropdown allows selection of multiple items.
sel.selectByIndex(0);
sel.selectByValue("option 5");
sel.selectByVisibleText("Interview");
sel.deselectByVisibleText("Interview");
sel.deselectByValue("option 5");
sel.deselectByIndex(0);
sel.deselectAll();
Syntax
The Thread.sleep() stops the test execution for a specific amount of time and the
Webdriver waits also to hold the execution for a specific amount of time till a condition is
met. If we are using both the waits, it will increase the overall execution time.
79. What happens when you use both implicit and explicit waits in
Selenium?
It is not recommended to add both the implicit and explicit waits in the same Selenium
tests. While adding both of them in one driver session, it results in uncertain wait time,
for example having a combination of explicit wait of 15 seconds and implicit wait of 10
seconds can bring a timeout error after 20 seconds.
An implicit wait is used only once in a test script and is applicable to every test step.
However, an explicit wait is used to wait for a particular condition. When both the waits
are being applied together, the explicit wait overwrites the implicit wait.
Page 32 of 44
Selenium is used to launch different browsers like Chrome, Firefox, Edge, Safari etc.
Syntax
// For Chrome
WebDriver driver = new ChromeDriver();
// For Firefox
WebDriver driver = new FirefoxDriver();
// For Edge
WebDriver driver = new EdgeDriver();
// For Safari
WebDriver driver = new SafariDriver();
The Actions class in Selenium is used to carry out keyboards and mouse operations.
These include carrying out double click, right click, click, pressing keys, drag and drag
actions etc. The Actions class comprises the advanced user interaction APIs.
Page 33 of 44
83. What are the areas where Selenium can contribute more for
testing?
The areas where Selenium can contribute more for testing are listed below −
Mobile Testing
Performance Testing
API Testing
Visual Testing
The testng.xml file is a part of the TestNG unit testing framework. It is used as a
configuration file to run the TestNG tests. The tests can be grouped, included or
excluded, executed in parallel etc with the help of the testng.xml file.
The assertions are verification points added to the test case to verify if the software is
working correctly as per the expectations. The assertions are added in the automated
test cases to confirm if a test case has passed or failed. The assertion methods are
available from the TestNG and the JUnit libraries.
Syntax
87. How to press the ENTER key within a text box using Selenium?
We can press the ENTER key within a text box using the sendKeys() method along with
the Key class. The Keys.ENTER is to be passed as a parameter to the sendKeys() method
to perform this action.
Syntax
// Identify element
WebElement m = driver.findElement(By.name("huj"));
m.sendKeys("Interview");
driver.get("https://www.tutorialspoint.com/selenium/index.htm");
driver.navigate().refresh();
driver.get("https://www.tutorialspoint.com/selenium/practice/text-
box.php");
WebElement t = driver.findElement(By.id("fullname"));
t.sendKeys(Keys.F5);
driver.get("https://www.tutorialspoint.com/selenium/practice/text-
box.php");
driver.get(driver.getCurrentUrl());
driver.get("https://www.tutorialspoint.com/selenium/practice/text-
box.php");
driver.navigate().to(driver.getCurrentUrl());
driver.get("https://www.tutorialspoint.com/selenium/practice/text-
box.php");
WebElement t = driver.findElement(By.id("fullname"));
t.sendKeys("\uE035");
We can assert the title of a page in Selenium with the help of the TestNG assertions.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import org.testng.Assert;
driver.get("https://www.tutorialspoint.com/selenium/practice/alerts.php
");
Page 36 of 44
Assert.assertEquals
(actualTitle, expectedTitle);
// Closing browser
driver.close();
}
}
A Selenium Maven project is one which uses Maven as a build tool and for getting the
correct dependencies to run the Selenium project. It also aids in creating a correct
project structure for the source code, reusable files, test code etc. Thus Maven helps to
achieve easier code maintenance, readability and reliability.
We can set the browser window size in Selenium with the help of the approaches listed
below −
driver.manage().window().maximize();
((JavascriptExecutor)driver).executeScript("window.resizeTo(500,
400)");
94. List the automation testing tools which can be integrated with
Selenium for continuous integration
Some of the automation testing tools which can be integrated with Selenium for
continuous integration are listed below −
Jenkins
CircleCI
AWS CodePipeline
Azure Devops
Bit Bucket
The implicit wait is a global wait applied to every test step in a test case. Its default wait
time is zero and it slows down the execution of tests. Also, it can cause false failure if the
element is detected after the given time specified in the implicit wait.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Page 38 of 44
import org.openqa.selenium.ie.InternetExplorerDriver;
import java.util.concurrent.TimeUnit;
driver.get("https://www.tutorialspoint.com/selenium/practice/modal-
dialogs.php");
// Closing browser
driver.quit();
}
}
Selenium Grid is used to support parallel execution across multiple browsers, platforms,
devices, and machines. It helps to reduce the execution time and allows efficient usage
of resources by distributing the execution in multiple machines. It is based on the hub-
node architecture.
// Identify element
WebElement element = driver.findElement
(By.xpath("//div/div/a"));
Actions actions = new Actions(driver);
actions.moveToElement(element ).contextClick().build().perform();
Page 40 of 44
Hybrid Framework
iTestListener
IReporter
IHookable
IExecutionListener
IConfugurable
IAnnotationTransformer
IConfigurationListener
IInvokedMethodListener
Selenium is used only for testing web based applications. To handle windows based
popups in Selenium, we have to integrate Selenium with AutoIt or Robot class.
The ExpectedConditions class is a utility class in Selenium that gives specific conditions
which should be satisfied before moving to the following step. These are used along with
the explicit waits and helps to achieve synchronization in tests. The ExpectedConditions
class is used along with the WebdriverWait class to wait for a particular time within which
the given criteria should be met, after that again the communication begins with the web
elements on the page.
Syntax
(By.id("id value")));
We can identify the login functionality of an application using the Selenium Webdriver.
First of all, we have to launch the application URL in a browser. Then we have to identify
the username, password, and the login button with the help of any of the locators
namely, id, class, name, css, xpath etc available in Selenium. Finally we have to input
the username and password with the help of the sendKeys() method and click the login
button using the click() method.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;
driver.get("https://www.tutorialspoint.com/selenium/practice/login.php"
);
Page 42 of 44
// identify element
WebElement username = driver.findElement(By.id("email"));
username.sendKeys("Selenium Java");
WebElement password = driver.findElement(By.id("password"));
password.sendKeys("Automation");
// identify element
WebElement loginBtn = driver.findElement
(By.id("//*[@id='signInForm']/div[3]/input"));
loginBtn.click();
// Closing browser
driver.quit();
}
}
The type command is used to enter keyboard key values in a text box. It also helps to
select an option in the combo box. The typeAndWait command is used when we are done
with typing and the web page starts loading. It holds back till the page is loaded before
moving to the next steps.
Code Implementation
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Page 43 of 44
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
driver.get("https://www.tutorialspoint.com/selenium/practice/login.php"
);
// Closing browser
driver.close();
}
}
WebDriverEventListener
AbstractWebDriverEventListener
Page 44 of 44
Syntax
try {
Yes it is possible to work with only build() without perform(). The build() method
constructs and returns an object of the Actions class, which is a collection of a series of
actions. If the build() method is not used, the actions are done immediately when
perform() is called.