2
2
You can
click on these question links to get to the answers directly.
How to locate an element by partially matching the attribute’s value using XPath?
How can we move to the parent of an element using XPath?
What is the fundamental difference between XPath and CSS selectors?
How to switch between multiple windows in Selenium?
Write the code to double-click an element.
What are some commonly encountered exceptions in Selenium?
How can we capture screenshots using Selenium?
What is the difference between driver.findElement() and driver.findElements()?
How to do drag and drop in Selenium?
Explain the line of code Webdriver driver = new FirefoxDriver();
1. What is Selenium?
Selenium is a robust test automation suite that is used for automating web-based applications. It
supports multiple browsers, programming languages, and platforms.
Selenium is open source and free to use without any licensing cost.
It supports multiple languages like Java, Ruby, Python, etc.
Selenium supports multi-browser testing.
It has vast resources and helping-community over the internet.
Using the Selenium IDE component, non-programmers can also write automation scripts.
Using the Selenium Grid component, distributed testing can be carried out on remote machines.
4. What are some limitations of Selenium?
Following are the limitations of Selenium–
Those who have used Selenium 3 and lower version would know that a Selenium test uses
JSON wire protocol to communicate with web browsers. In the case of Selenium 4 there is no
need for encoding and decoding the API requests using the JSON wire protocol for
communication between browsers and test scripts. This allows the WebDriver to interact directly
with the target browser.
Id
XPath
CSS selector
className
tagName
name
link text
partialLinkText
10. How can we inspect the web element attributes in order to use them in different locators?
In order to locate web elements, we can use the Developer tool and plugins like Firebug.
We can launch the developer tool by pressing F12 on the browser. Users can easily hover over
any element and find its different HTML properties.
Firebug is a plugin of Firefox that provides various development tools for debugging
applications. From an automation perspective, we use Firebug specifically for inspecting web
elements in order to find their attributes like id, class, name, etc. in different locators.
The main disadvantage of absolute XPath is that even if there is a slight change in the UI or any
element, the whole XPath will fail.
Example – html/body/div/div[2]/div/div/div/div[1]/div/input
In this way, there are different ways of creating robust relative XPaths that have minimal or no
change with the changes in other UI elements.
Example – //input[@id=’username’]
14. What is the difference between a single slash(/) and a double slash(//) in XPath?
In XPath, a single slash is used for creating absolute XPaths, beginning from the root node.
Whereas double slash is used for creating relative XPaths.
15. How can we locate an element by only partially matching the value of its attributes in Xpath?
Using contains() method we can locate an element by partially matching its attribute’s value.
This is particularly helpful in scenarios where the attributes have dynamic values with a certain
constant part.
xPathExpression = //*[text()='username']
17. How can we move to the parent of an element using XPath?
Using ‘/..’ after the XPath expression of the child element, we can move to the parent of an
element.
For example, the locator //div[@id=”childId”]/.. will move to the parent of the div element with id
value as ‘childId’.
19. What is the syntax of finding elements by class using CSS Selector?
By using .className in the CSS locator, we can select all the elements belonging to a particular
class e.g. ‘.red’ will select all elements having class ‘red’.
21. How can we select elements by their attribute value using the CSS Selector?
Using [attribute=value] in the CSS locator, we can select all the elements belonging to a
particular class e.g. ‘[type=small]’ will select the element having attribute type of value ‘small’.
22. How can we move to the nth-child element using the CSS selector?
Using :nth-child(n) in the CSS locator, we can move to the nth child element e.g. div:nth-child(2)
will locate the 2nd div element of its parent.
23. What is the fundamental difference between XPath and CSS selectors?
The fundamental difference between XPath and CSS selector is – using XPaths we can
traverse up in the document i.e. we can move to parent elements. Whereas using the CSS
selector, we can only move downwards in the document.
Selenium interview questions
Selenium Java Interview Questions
In this section, we will primarily talk about the Selenium WebDriver interview questions based on
Java programming language.
driver.navigate() allows moving back and forward in browser history with the help of
driver.navigate().forward() and driver.navigate().back() commands.
In the case of single-page applications (where the URL is appended by ‘#’ to navigate to
different sections of the page)-
driver.navigate().to() navigates to a particular section by changing the URL without refreshing
the page.
Whereas driver.get() refreshes the page.
This refreshing of the page is also the primary reason why history is not maintained in the case
of the driver.get() command.
Reference – Stack overflow
driver.findElement(By.id("elementLocator")).clear();
28. How to check a checkbox in Selenium?
The same click() method that we use for clicking buttons or radio buttons can be used for
checking the checkbox as well.
29. How can we submit a form in Selenium?
Using the submit() method we can submit a form in selenium.
driver.findElement(By.id("form1")).submit();
Also, we can use the click() method for the same purpose.
driver.switchTo().frame("{frameIndex/frameId/frameName}");
For locating a frame, we can either use the index (starting from 0), its name, or its Id.
34. Can we move back and forward in the browser using Selenium?
Yes, using driver.navigate().back() and driver.navigate().forward() commands, we can move
backward and forward in a browser.
driver.manage().window().maximize();
37. How can we fetch a text written over an element?
Using the getText() method we can fetch the text over an element.
String valueAttribute =
driver.findElement(By.id("locator")).getAttribute("value");
39. How to delete cookies in Selenium?
Using deleteAllCookies() method.
driver.manage().deleteAllCookies();
40. What is an implicit wait in Selenium?
An implicit wait is a type of wait that waits for a specified time while locating an element before
throwing NoSuchElementException. By default, Selenium tries to find web elements
immediately when required without any wait. So, it is good to use implicit wait. This wait is
applied to all the elements of the current driver instance.
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
41. What is an explicit wait in Selenium?
An explicit wait is a type of wait that is applied to a particular web element until the expected
condition specified is met.
Check our detailed tutorial here – Implicit & Explicit Waits in Selenium.
.withTimeout(20, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement textBox = wait.until(new Function<webdriver,webElement>() {
return driver.findElement(By.id("textBoxId"));
}
}
);
44. What are the different keyboard operations that can be performed in Selenium?
The different keyboard operations that can be performed in Selenium are-
45. What are the different mouse actions that can be performed using Selenium?
The different mouse events supported in Selenium are-
click(WebElement element)
doubleClick(WebElement element)
contextClick(WebElement element)
mouseDown(WebElement element)
mouseUp(WebElement element)
mouseMove(WebElement element)
mouseMove(WebElement element, long xOffset, long yOffset)
driver.getCurrentUrl();
50. How can we fetch the title of the page in Selenium?
Using driver.getTitle() command, we can fetch the page title in Selenium. This method returns a
string containing the title of the webpage.
driver.findElement(By locator).isDisplayed();
61. How can we check if an element is enabled for interaction on a web page?
Using the isEnabled method, we can check if an element is enabled or not.
driver.findElement(By locator).isEnabled();
62. What is the difference between driver.findElement() and driver.findElements() commands?
The difference between driver.findElement() and driver.findElements() commands are-
findElement() returns a single WebElement (found first) based on the locator passed as a
parameter. Whereas findElements() returns a list of WebElements, all satisfying the locator
value passed.
Syntax of findElement()-
WebElement textbox = driver.findElement(By.id(“textBoxLocator”));
Another difference between the two is – if no element is found then findElement() throws
NoSuchElementException whereas findElements() returns a list of 0 elements.
Selenium interview questions and answers
63. How can we handle window UI elements and window POP-ups using selenium?
Selenium is used for automating web-based applications only(or browsers only). If we want to
handle window GUI elements then we can use tools like AutoIT.
AutoIT is a freeware used for automating Windows GUI. The AutoIt scripts follow the simple
BASIC language-like syntax. Also, it can be easily integrated with Selenium tests.
Using element.sendKeys(“path of file”) on the web element of ‘input’ tag and type ‘file’ i.e. the
elements should be like –
<input type=”file” name=”fileUpload”>
With the help of Robot API.
Using the AutoIT API.
66. How to handle the HTTPS website in Selenium or how to accept the SSL untrusted
connection?
Using profiles, we can handle accepting the SSL untrusted connection certificate. Profiles are
basically a set of user preferences stored in a file.
(JavascriptExecutor(driver))
.executeScript("document.getElementsByClassName(locator).click();");
Now coming to POM – POM helps to create a framework for maintaining selenium scripts. In
POM for each page of the application, a class is created having the web elements belonging to
the page and methods handling the events on that page. The test scripts are maintained in
separate files and the methods of the page object files are called from the test scripts file.
Using POM, we can create an Object Repository i.e. a set of web elements in separate files
along with their associated functions. In this way, keeping the code clean.
For any change in UI(or web elements), only page object files are required to be updated
leaving test files unchanged.
It makes code reusable as well as maintainable.
74. What is Page Factory?
Page factory is an implementation of the Page Object Model in Selenium. It provides @FindBy
annotation to find web elements. In addition, there is a PageFactory.initElements() method to
initialize all web elements defined with @FindBy annotation.
@FindBy(name="searchBtn")
WebElement searchButton;
//Constructor
public samplePage(WebDriver driver){
this.driver = driver;
//initElements method to initialize all elements
PageFactory.initElements(driver, this);
}
//Sample method
public void search(String searchTerm){
searchTextBox.sendKeys(searchTerm);
searchButton.click();
}
}
75. What is an Object repository?
An object repository is the centralized location of all the objects or WebElements of the test
scripts. In Selenium, we can implement an object repository using the Page Object Model as
well as Page Factory design patterns.
TestNG provides inherent support for data-driven testing using @dataProvider annotation.
In this way, a person of a non-programming background can also write the test steps in a file as
long as all the keywords are present in the framework along with the implementation.
It allows running test cases in parallel thereby saving test execution time.
Multi-browser testing is possible using the Selenium grid by running the test on machines
having different browsers.
Additionally, we can do multi-platform testing by configuring nodes having different operating
systems.
84 What is the purpose of creating a reference variable- ‘driver’ of type WebDriver instead of
directly creating a FireFoxDriver object or any other driver’s reference in the statement
Webdriver driver = new FirefoxDriver();?
By creating a reference variable of type WebDriver, we can use the same variable to work with
multiple browsers like ChromeDriver, IEDriver, etc.
85. Name an API used for reading and writing data to Excel files.
Apache POI API and JXL(Java Excel API) can be used for reading, writing, and updating Excel
files.
TestNG provides different assertions that help in checking the expected and actual results.
It provides parallel execution of test methods.
We can define the dependency of one test method over others in TestNG.
Also, we can assign priority to test methods in selenium.
It allows the grouping of test methods into test groups.
It allows data-driven testing using @DataProvider annotation.
TestNG has inherent support for reporting.
It has support for parameterizing test cases using @Parameters annotation.
You can also look for What is TestNG?
assertEquals(String actual, String expected, String message) – (and other overloaded data
types in parameters)
assertNotEquals(double data1, double data2, String message) – (and other overloaded data
type in parameters)
assertFalse(boolean condition, String message)
assertTrue(boolean condition, String message)
assertNotNull(Object object)
fail(boolean condition, String message)
true(String message)
93. How can we pass the parameter to the test script using TestNG?
Using @Parameter annotation and the ‘parameter’ tag in testng.xml we can pass parameters to
the test script.
Sample testng.xml –
<suite name="sampleTestSuite">
<test name="sampleTest">
<parameter name="sampleParamName" value="sampleValue"/>
<classes>
<class name="TestFile" />
</classes>
</test>
</suite>
Sample test script-
@Listeners(PackageName.CustomizedListenerClassName.class)
public class TestClass {
WebDriver driver= new FirefoxDriver();
@Test
public void testMethod(){
//test logic
}
}
96. How can we make one test method dependent on others using TestNG?
Using the ‘dependsOnMethods’ parameter inside @Test annotation in TestNG, we can make
one test method run only after the successful execution of the dependent test method.
@Test(dependsOnMethods = { "preTests" })
97. How can we set the priority of test cases in TestNG?
Using the priority parameter in @Test annotation in TestNG we can define the priority of test
cases. The default priority of the test when not specified is the integer value 0. Example-
@Test(priority=1)
98. What is the default priority of a test method in TestNG?
The default priority of a test when not specified is integer value 0. So, if we have one test case
with priority 1 and one without any priority then the test without any priority value will execute
first.
parallel=”{methods/tests/classes}”
thread-count=”{number of threads you want to run simultaneously}”.
<suite name="ArtOfTestingSuite" parallel="methods" thread-count="5">
Check Running Selenium Tests in parallel for details.
For example – there are two classes TestClass and the TestFactory class. Because of the
@Factory annotation, the test methods in class TestClass will run twice with the data “k1” and
“k2”.
@Test
public void TestMethod() {
System.out.println(str);
}
}
WebElement dynamicElement =
driver.findElement(By.xpath("//input[contains(@id, 'partialId that is static')]"));
2. Using parent elements–
Sometimes, we can locate a stable parent element and then navigate to the dynamic element
within it.
WebElement parentElement =
driver.findElement(By.id("staticParentElementId"));
WebElement dynamicElement =
parentElement.findElement(By.tagName("button"));
In case of web elements that appear with some delay on the web page, we can use different
waits like – implicit wait, explicit wait (preferred as it has waits based on different expected
conditions) and fluent wait.
104. Explain the concept of parallel test execution in Selenium. What tools or frameworks can
be used for parallel execution?
Parallel test execution involves running multiple test cases simultaneously to save the test
execution time. Tools and frameworks like TestNG, JUnit, and Selenium Grid can be used for
parallel execution.
106. Explain the concept of headless browsers in Selenium. In what situation can we use
headless browsers?
Headless browsers (e.g., Headless Chrome) offer faster test execution, lower resource
consumption, and provide the ability to run tests on servers without a graphical interface. They
are suitable for automated testing, web scraping, and continuous integration environments
where GUI rendering is unnecessary.
107. Can you discuss the best practices for writing maintainable and robust Selenium
automation scripts?
Best practices for writing robust test scripts include using a design pattern like – Page Object
Model, implementing proper waits, keeping test data separate from test logic, using meaningful
test case names, and regularly refactoring code to maintain readability and reliability.
108. How do you handle synchronization issues in Selenium when dealing with asynchronous
web applications?
Asynchronous web applications can be challenging to automate due to delayed element
loading. We can use explicit waits, ExpectedConditions, and custom wait conditions to handle
synchronization issues. You may need to wait for elements to become clickable, visible, or have
specific attributes/values before interacting with them.
109. Explain how you would handle cross-browser testing using Selenium. What challenges can
arise in this context?
Cross-browser testing involves running tests on different browsers (e.g., Chrome, Firefox,
Safari, Edge, etc) to ensure browser compatibility. Selenium Grid and WebDriver’s support for
multiple browser drivers helps in cross-browser testing.
Browser Diversity – Different browsers have distinct rendering engines (e.g., Chrome’s Blink,
Firefox’s Gecko, Safari’s WebKit), leading to variations in how they display web content. At
times, we have to write browser-specific code to handle different web elements.
Browser Versions – Each browser frequently releases new versions with updates and
improvements. So, we have to make sure that the test scripts work fine with the new version
while also supporting the older versions.
Platform Differences – Browsers may behave differently on different operating systems
(Windows, macOS, Linux). Cross-browser testing should account for these platform variations to
ensure a seamless experience for all users.
110. What is the role of the WebDriverManager library in Selenium automation, and why is it
useful?
WebDriverManager is a library that automates the download and setup of WebDriver binaries
for different browsers and versions. It simplifies WebDriver setup and ensures that the correct
driver version is used, reducing compatibility issues and maintenance efforts.