0% found this document useful (0 votes)
5 views31 pages

Selenium WebDriver Java Interview Questions

The document contains a comprehensive list of interview questions and answers related to Selenium WebDriver with Java, covering topics such as locator strategies, exceptions, automation testing practices, and various Selenium functionalities. It explains the use of Selenium in testing web applications, the types of waits, handling alerts, and the differences between various assertion types. Additionally, it provides coding examples and discusses the integration of Selenium with other tools and frameworks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views31 pages

Selenium WebDriver Java Interview Questions

The document contains a comprehensive list of interview questions and answers related to Selenium WebDriver with Java, covering topics such as locator strategies, exceptions, automation testing practices, and various Selenium functionalities. It explains the use of Selenium in testing web applications, the types of waits, handling alerts, and the differences between various assertion types. Additionally, it provides coding examples and discusses the integration of Selenium with other tools and frameworks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Selenium WebDriver with Java Interview questions:

1. What are the different types of Locator Strategy supported by


Selenium?

Solution:

Selenium supports almost all types of locators.

• Class name: Locates the elements using Class Name


• CSS Selector: Locates the Element using CSS selectors
• ID: Locates the Element using the ID attribute
• Name: Locates elements whose NAME attribute matches the search
value
• Link Text: Locates anchor elements whose visible text matches the
search value
• Partial Link Text: Locates anchor elements whose visible text
contains the search value. If multiple elements are matching, only the
first one will be selected.
• Tag Name: Locates elements whose tag name matches the search
value
• Xpath: Locates elements matching an XPath expression

For more information, please visit: [Link]/Blog


2. Explain at least 5 different types of exceptions in Selenium.

Solution:

NoSuchElementException
Reason: The exception occurs when WebDriver is unable to find and locate
elements. The incorrect locator can cause this exception in selenium.

TimeoutException
Reason: Selenium waits until a specific time period, If the element could not
display or load in specified time.

WebDriverException:
Reason: Webdriver performs actions immediately after ‘closing’ the browser or it
can also occur when our code unable to initialize the webdriver

ElementNotVisibleException
Reason: Selenium tries to perform action on invisible Elements

NoSuchSessionException
Reason: When webdriver cannot execute the commands using driver instance.

NoAlertPresentException
Reason: The user is trying to handle an alert that is not present.

NoSuchWindowException
Reason: WebDriver tries to switch to an invalid window.

For more information, please visit: [Link]/Blog


3. I have a use case that I need to execute some of the tests only once, I
don’t have to execute these tests in the future, Should I use selenium
WebDriver for this?

Solution:
The Standard Practice for Automation Testing is to Automate only repetitive
tasks, as creating and setting up Automation Tests involves cost and effort. In the
above use case, the test is performed only once so it is not recommended to use
automation.

4. Can we use Selenium for Product Development? If not, what is


selenium used for?

Solution:
Selenium is Not a Product Development Framework; Selenium is
Automation Testing Framework. Selenium can be used for testing Web
Applications.

For more information, please visit: [Link]/Blog


5. Explain Explicit Implicit and Fluent waits in Selenium?

Solution:

For more information, please visit: [Link]/Blog


6. What is Selenium IDE? when should we use it?

Solution:
The Selenium-IDE (Integrated Development Environment) is the tool, used
to develop your Selenium test cases. Selenium IDE is useful for beginners, where
they want to write less code and actions can be done using User Interface (UI).
7. What is Automation Testing?

Solution:

Automation Testing is Software Testing Technique where the actual and


expected outcome is validated using Programmatically using Automation Tools.

For more information, please visit: [Link]/Blog


8. What is Selenese?

Solution:

Selenium is the Web Automation Tool, which provides a set of Selenium


commands to test the web applications. These Selenium Commands are often
referred to as Selenese.

9. Explain List of Supported Drivers by Selenium?

Solution:

Selenium Communicates with browser using drivers, Each browser has its
specific drivers.

• Chrome Driver: Chrome/ Chromium based browsers


• GeckoDriver: Firefox Browsers
• InternetExplorerDriver: Internet Explorer Browser
• Safari Driver: Safari Browser
• Edge Driver: Edge Browser

10. How to read data from excel in selenium webdriver?

Solution:

Selenium Webdriver doesn’t provide any functionality to work with an


excel file. However, reading and writing data into excel can be done using Java
Utility called POI
Apache POI is an open-source java library used to handle Microsoft Office-based
files. This helps the user to perform the operation on excel files such as read,
write, etc.

For more information, please visit: [Link]/Blog


Once you add the Apache POI jar to your project you can use the library utilities
to read/write data into Excel File.
Example:
FileInputStream fs = new FileInputStream("D:\\[Link]");
XSSFWorkbook workbook = new XSSFWorkbook(fs);
XSSFSheet sheet = [Link](0);
Row row = [Link](0);
Cell cell = [Link](0);
[Link]([Link](0).getCell(0));

11. What is the difference between Assert and Verify.

Solution:

Assert: If the condition fails, then the test will be terminated. There will
not be further execution of the test step
Verify: If the condition fails, Execution will continue as normally there will be
error logs. Execution will not be terminated.

12. Which method is the overloaded method selenium webdriver?

Solution:

The most used overloaded method in Selenium is:

• frame() Method: This method is helpful when we work with iFrame.


Based on the parameter we pass Index, WebElement, or name the
respective implementations are called.
• Example:
• By Index
• [Link]().frame(0);
• By Name or Id
• [Link]().frame("iframe1")

For more information, please visit: [Link]/Blog


• By Web Element
• WebElement iframe = [Link]([Link]("Frame1"));
• [Link]().frame(iframe);

13. How to handle window dialog such as File Open Dialog in Windows
using Selenium?

Solution:

Selenium supports only Web Application Automation, Window-based


dialog cannot be automated using Selenium. However integrating with tools like
Autoit, Robot Class will be helpful in this case.

14. What is By in selenium?

Solution:

By is the class in Selenium, it provides the locater specific methods and


can be used with [Link](s) to get the Element.
List of Important Methods provided by By Class are
Id()
linkText()
partialLinkText()
name()
tagName()
xpath()
className()
cssSelector()

For more information, please visit: [Link]/Blog


15. What are Hard and Soft Asserts in selenium?

Solution:

Hard Assert: Hard assert throws the exception as soon as the condition
fails and execution for the current test case will be stopped and continues with
the next test case execution.
Soft Assert: Soft Assert doesn’t throw an exception when the condition fails.

16. How to handle hidden element in Selenium?

Solution:

A hidden element means that Element is present in DOM but it is not


visible in the browser. To handle the hidden element in Selenium, we need to use
the JavaScript Executor.
For Example:
Consider we have a hidden text box with id="hiddenbox1".
JavascriptExecutor executor = (JavascriptExecutor) driver;
String str = (String) [Link]("return
[Link]('hiddenbox1').value");
Though, the hiddenbox1 is not visible on the browser, since the text box is
attached to DOM we can get the value of the text box using the above method.

17. What is the Action Class in Selenium?

Solution:

Action classes provide the ability to simulate mouse and keyboard events.
Example Mousehover, Drag and drop, etc.
Actions action = new Actions(driver);
[Link](element).click().perform();

For more information, please visit: [Link]/Blog


18. What are the different types of frameworks?

Solution:

Page Object Model: Page Object Model (POM), is a design pattern in


Selenium that creates an object repository for storing all web elements. It is
useful in reducing code duplication and maintenance. In-Page Object Model, Each
web page of an application as a class file. Each class file will contain only
corresponding web page elements
Data-Driven Framework: When the entire test data is generated from some
external files like Excel, CSV, XML, or some database table, then it is called Data
Driven framework
Keyword Driven Framework: When only the instructions and operations are
written in a different file like an Excel worksheet, it is called Keyword Driven
framework.
Hybrid Framework: Any combination above frameworks like POM, Data-
Driven, Keyword Driven is called a Hybrid Framework.

19. Explain Selenium Browser Naivigation commands.

Solution:

[Link]() : Used to navigating to webpage


Example:
[Link]("[Link]
[Link](): Reads the current URL from the browser’s address.
[Link]().back(); : Navigates back to previous page. Equivalant to
clicking browser back button.
[Link]().forward() : Navigates forwards way. Equivalant to clicking
browser forward button.
[Link]().refresh(); : Refreshs the current page

For more information, please visit: [Link]/Blog


20. Give an example to perform drag and drop action In Selenium
WebDriver?

Solution:

Actions actions = new Actions(driver);


WebElement from = [Link]([Link]("column-a "));
WebElement to = [Link]([Link]("column-b"));
[Link](from, to).build().perform();

21. How can I switch to Multiple Windows in Selenium?

Solution:

Selenium supports working with Multiple Tabs and Windows. switchTo()


method is mainly used for Switching between Tabs or windows.
ArrayList<String> newTab = new
ArrayList<String>([Link]());
//switch to new tab
[Link]().window([Link](1));

For more information, please visit: [Link]/Blog


22. How can I Capture Screenshot in Selenium 4?

Solution:

//Taking screenshot of WebPage


WebDriver driver = new ChromeDriver();
[Link]("[Link]
File scrFile = ((TakesScreenshot)driver).getScreenshotAs([Link]);
[Link](scrFile, new File("./[Link]"));
//Taking screenshot of WebElement
WebDriver driver = new ChromeDriver();
[Link]("[Link]
WebElement element = [Link]([Link]("h1"));
File scrFile = [Link]([Link]);
[Link](scrFile, new File("./[Link]"));

23. What are the different languages supported by Selenium?

Solution:

Selenium Supports Following Languages

• C#
• Java
• Python
• Ruby
• Javascript

For more information, please visit: [Link]/Blog


24. What is Selenium Webdriver?

Solution:

Selenium Webdriver is an Open source tool that contains a collection of


APIs which is used to verify the result of test cases programmatically.
Or.
Selenium Webdriver is an Open Source Automation Tool, which contains a
collection of API’s which is used to test the web application using supported
programming languages.

25. How to handle alerts pop up in Selenium?

Solution:

[Link]().alert() is used for Switching to Browser Alert.


Accept Alert :
[Link]().alert().accept();
Dismiss Alert:
[Link]().alert().dismiss();
Get Text:
[Link]().alert().getText();
Type Text:
[Link]().alert().sendKeys("Text");

For more information, please visit: [Link]/Blog


26. Write a Small Code Snippet Launch a browser and Navigate to
Webpage and Close the browser.

Solution:

[Link]("[Link]","C://[Link]");
driver= new ChromeDriver();
[Link]("[Link]

27. Explain Javascript Executor in Selenium.

Solution:

Javascript executor helps to execute Javascript on the browser. Sometimes


the default simulation events might not work click(), getText(), etc. Using
Javascript executor we can make it work.
Example:
JavascriptExecutor js = (JavascriptExecutor) driver;
[Link]("[Link]('element id ').click();");

28. Can I Create a Selenium Java framework without using TestNG?

Solution:

TestNG and Selenium are two different things, TestNG helps us to manage
Assertions, Reporters and makes things easier for creating the complete
framework. We can use other frameworks like Junit to create Selenium
Framework.

For more information, please visit: [Link]/Blog


29. Does Selenium support IFrames, If so please explain.

Solution:

• Selenium Supports Iframe, We can Switch To and From Iframe.


• Example Code
• We can Switch to Iframe using the below techniques.
• By Index
• Example:
[Link]().frame(0);

• By Name or Id
[Link]().frame("iframe1")

• By Web Element
WebElement iframe = [Link]([Link]("Frame1"));
[Link]().frame(iframe);

30. I need to press Keys CTRL + SHIFT + S how can I do that in Selenium?

Solution:

For more information, please visit: [Link]/Blog


31. Write a Java Program to find the longest consecutive occurrence of
integers in a given array.

Solution:
In this problem, we need to find the length of the longest consecutive
occurrence.
As given in the array below, we have an array of integers. As you can see the
longest consecutive occurrence of integers are 6,7,8,9. There is a consecutive
increment of 1 . So the output will be 4.

For more information, please visit: [Link]/Blog


The above program will give the output as 4. We use the [Link]() to obtain
the maximum value between the max and the counter variable.

32. Write a Java Program to reverse a String.

Solution:

As asked in the above question, we need to reverse a String. For Ex:


The Input is : "RahulShettyAcademy"
Output should be : "ymedacAyttehSluhaR"
This problem can be solved by writing a simple for loop and print the string in
the reverse.

For more information, please visit: [Link]/Blog


This will print the outputString which will be the reverse of the given
inputString.

33. Write a Java Program to get the count of Capitalized words in a


String.

Solution:
Capitalized words in a given String which are those words which starts
with a Capital Letter.
We need the total count of these words. This problem can be solved by using a
for loop and iterate through each character in the String.
We also take a counter variable and increment it when any Capital character is
encountered.

The above program will return the total count of Capitalized String i.e. 3.

For more information, please visit: [Link]/Blog


34. Write a Java Program to swap two given Strings.

Solution:

As asked in the above question, for swapping of two strings we need to


use the substring method of the String class in Java.
Substring method returns part of the string.
We have the start index and end index in the substring method where
start index is inclusive and end index is exclusive.
Below is the Java Program to swap two Strings.

The above program will swap the two Strings s1 and s2.

For more information, please visit: [Link]/Blog


35. Write a Java Program to reverse an array?

Solution:

In this problem, we are given an array of characters as input. This


problem can be solved by writing a while loop.
By using a third variable, we reverse the array as below.

The above program will return the total count of Capitalized String i.e. 3.

For more information, please visit: [Link]/Blog


36. Write a Java Program to convert HashMap to ArrayList.

Solution:

To solve this problem, we can convert the HashMap keys into an ArrayList
and then the HashMap values into an ArrayList.

For more information, please visit: [Link]/Blog


37. Write a Java Program to print the product of an array except self?

Solution:

In this problem, We need the output which is the product of the all the
numbers in the array except self.
Here we take two arrays, left_products and right_products in which we store the
products of all numbers except self.
Then we multiply the left_products and right_products to get the output.

Below is the Java Program:

For more information, please visit: [Link]/Blog


38. Write a Java Program to generate Output "aabbbcccc" with the input
"a2b3c4"

Solution:

In the above problem, we have the numbers in the input. We need to print
the as many characters same the number value.

For more information, please visit: [Link]/Blog


39. Write a Java Program to print the second largest number in a given
array.

Solution:

In this problem, to find the second largest number in the array, first we
sort the array and then using the for loop we find the second largest number in
reverse order.

Below is the Java Program:

For more information, please visit: [Link]/Blog


The output of the above program is 6. In this it also checks that the second
largest number is not just based on index. It should be the second largest number
in the array.

40. Write a Java Program to print numbers 1 to 100 without using any
loop(for/while/do-while).

Solution:

In this problem, to print the numbers between 1 to 100 we will use the if
statement and check the condition.
We will check the condition i.e. num value should be less than equal to 100 and
then increment it till it reaches to 100 and thus printing each number.

For more information, please visit: [Link]/Blog


41. What are the limitations of Selenium testing?
Solution:
Unavailability of reliable tech support: Since Selenium is an open-source tool, it
does not have dedicated tech support to resolve the user queries.

1. Tests web applications only: Selenium needs to be integrated with third-


party tools like Appium and TestNG to test desktop and mobile
applications.

2. Limited support for image testing.

3. No built-in reporting and test management facility: Selenium has to be


integrated with tools like TestNG, or JUnit among others to facilitate test
reporting and management.

4. May require the knowledge of programming languages: Selenium


WebDriver expects the user to have some basic knowledge about
programming.

42. What are the testing types supported by Selenium?

Solution:
Selenium supports Regression testing and Functional testing.

Regression testing - It is a full or partial selection of already executed test cases


that are re-executed to ensure existing functionalities work fine.

The steps involved are -

Re-testing: All tests in the existing test suite are executed. It proves to be very
expensive and time-consuming.

For more information, please visit: [Link]/Blog


Regression test selection: Tests are classified as feature tests, integration tests,
and the end to end tests. In this step, some of the tests are selected.

Prioritization of test cases: The selected test cases are prioritized based on
business impact and critical functionalities.

Functional testing - Functional Testing involves the verification of every function


of the application with the required specification.

The following are the steps involved:

Identify test input


Compute test outcome
Execute test
Compare the test outcome with the actual outcome

43. Mention the types of navigation commands?

Solution:

[Link]().to("[Link] - Navigates to
the provided URL

[Link]().refresh(); - This method refreshes the current page

[Link]().forward(); - This method does the same operation as clicking


on the Forward Button of any browser. It neither accepts nor returns anything.

[Link]().back(); - This method does the same operation as clicking on


the Back Button of any browser. It neither accepts nor returns anything.

For more information, please visit: [Link]/Blog


44. What is the major difference between [Link]() and
[Link]()?

Solution:
[Link]()
This command closes the browser’s current window. If multiple windows are
open, the current window of focus will be closed.
[Link]()
When quit() is called on the driver instance and there are one or more browser
windows open, it closes all the open browser windows.

45. How to type text in an input box using Selenium?

Solution:
sendKeys() is the method used to type text in input boxes
Consider the following example -
WebElement email = [Link]([Link](“email”)); - Finds the “email” text
using the ID locator
[Link](“[Link]@[Link]”); - Enters text into the URL field
WebElement password = [Link]([Link](“Password”)); - Finds the
“password” text using the ID locator
[Link](“abcdefgh123”); - Enters text into the password field

For more information, please visit: [Link]/Blog


46. How to click on a hyperlink in Selenium?

Solution:

[Link]([Link](“Today’s deals”)).click();

The command finds the element using link text and then clicks on that element, where
after the user would be redirected to the corresponding page.

[Link]([Link](“Service”)).click();

The above command finds the element based on the substring of the link provided in
the parenthesis and thus partialLinkText() finds the web element.

47. How many types of memory areas are allocated by JVM?

Solution:
Class(Method) Area: Class Area stores per-class structures such as the runtime
constant pool, field, method data, and the code for methods.
Heap: It is the runtime data area in which the memory is allocated to the objects
Stack: Java Stack stores frames. It holds local variables and partial results, and plays a
part in method invocation and return. Each thread has a private JVM stack, created at
the same time as the thread. A new frame is created each time a method is invoked. A
frame is destroyed when its method invocation completes.
Program Counter Register: PC (program counter) register contains the address of the
Java virtual machine instruction currently being executed.
Native Method Stack: It contains all the native methods used in the application.

For more information, please visit: [Link]/Blog


48. What is JIT compiler?

Solution:
Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts
of the bytecode that have similar functionality at the same time, and hence reduces the
amount of time needed for compilation.
Here the term “compiler” refers to a translator from the instruction set of a Java virtual
machine (JVM) to the instruction set of a specific CPU.

49. What is classloader?

Solution:

Classloader is a subsystem of JVM which is used to load class files. Whenever we


run the java program, it is loaded first by the classloader. There are three built-in
classloaders in Java.
Bootstrap ClassLoader: This is the first classloader which is the superclass of
Extension classloader. It loads the [Link] file which contains all class files of Java
Standard Edition like [Link] package classes, [Link] package classes, [Link]
package classes, [Link] package classes, [Link] package classes, etc.
Extension ClassLoader: This is the child classloader of Bootstrap and parent
classloader of System classloader. It loads the jar files located inside
$JAVA_HOME/jre/lib/ext directory.
System/Application ClassLoader: This is the child classloader of Extension
classloader. It loads the class files from the classpath. By default, the classpath is
set to the current directory. You can change the classpath using "-cp" or "-
classpath" switch. It is also known as Application classloader.

For more information, please visit: [Link]/Blog


50. What if I write static public void instead of public static void?

Solution:
The program compiles and runs correctly because the order of specifiers doesn't
matter in Java.

For more information, please visit: [Link]/Blog

You might also like