Selenium is an important part of Software Testing, and in Software Testing interviews, Selenium plays a very important role in testing knowledge related to the same.
This article includes the Selenium Interview Questions, which include the basics to advanced related to Selenium Automation concepts.
Beginner-Level Selenium Interview Questions
1. What is Selenium?
Selenium is a widely used tool for Testing Web-Based Applications that checks if they are doing as expected. It is a prominent preference amongst testers for Cross-Browser Testing and is viewed as one of the most reliable systems for Web Application Automation evaluation.
Selenium is also platform-independent, so it can provide distributed testing using the Selenium Network. Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages.
Selenium Features2. What are the Selenium suite components?
Here are four major Components of Selenium:
Components of Selenium1. Selenium IDE
Selenium IDE serves as an innovative toolkit for web testing, allowing users to record interactions with web applications. Selenium-IDE was initially created by "Shinya Kasatani" in 2006. Selenium IDE also helps to simplify the testing process. It is a friendly space for testers and developers to team up. This helps everyone quickly share important testing information and results, making things work better and feel accomplished.
2. Selenium RC (Remote control)
Selenium Remote Control (RC) was one of the earliest Selenium Tools, preceding WebDriver. It allowed testers to write automated web application tests in various programming languages like Java, C#, Python, etc. The key feature of Selenium RC was its ability to interact with web browsers using a server, which acted as an intermediary between the testing code and the browser.
3. Selenium Web Driver
Selenium WebDriver is a robust Open-Source Framework for Automating Web Browsers, primarily aimed at easing the testing and verification of Web Applications. As an important part of the Selenium suite, WebDriver offers a programming interface to interact with web browsers, allowing developers and testers to automate browser actions seamlessly.
4. Selenium GRID
Selenium Grid is a server that allows tests to use web browser instances running on remote machines. With Selenium Grid, one server acts as the hub. Tests contact the hub to obtain access to browser instances.
Selenium mainly used among the automation tools due to its unique benefits, including with being the open-source framework, it will be supporting multiple languages and platforms, and offering easy compatibility with the various browsers, providing flexible frameworks for many more use of the same, It will benefit in enabling reusability, and improving the integrated and parallel test execution.
4. What is test automation or automation testing?
Automated Testing means using special software for tasks that people usually do when checking and testing a software product. Nowadays, many software projects use automation testing from start to end, especially in Agile and DevOps methods
5. What are the advantages of automation testing?
- Simplifies Test Case Execution: Automation testing can be left virtually unattended and thus it allows monitoring of the results at the end of the process. Thus, simplifying the overall test execution and increasing the efficiency of the application.
- Improves Reliability of Tests: Automation testing ensures that there is equal focus on all the areas of the testing, thus ensuring the best quality end product.
- Increases amount of test coverage: Using automation testing, more test cases can be created and executed for the application under test. Thus, resulting in higher test coverage and the detection of more bugs. This allows for the testing of more complex applications and more features can be tested.
- Minimizing Human Interaction: In automation testing, everything is automated from test case creation to execution thus there are no changes for human error due to neglect. This reduces the necessity for fixing glitches in the post-release phase.
- Saves Time and Money: The initial investment for automation testing is on the higher side but it is cost-efficient and time-efficient in the long run. This is due to the reduction in the amount of time required for test case creation and execution which contributes to the high quality of work.
- Earlier detection of defects: Automation testing documents the defects, thus making it easier for the development team to fix the defect and give a faster output. The earlier the defect is identified, the more easier and cost-efficient it is to fix the defects.
6. What is Selenese? How is it classified?
Selenese are the sets of commands that are used in Selenium ide to perform various operations on the web. It is majorly used to develop coding scripts in Selenium IDE. The element locator helps Selenium to decide which HTML element a command will refer to.
Selenium commands- Actions: Actions are selenium IDE commands that control the status of an application.
- Accessors: Accessors are the selenium commands that are used to check the state of the application and store the result in a variable.
- Assertions: Assertions are the selenium commands that enable the testers to check/verify the status of an application.
7. What is the difference between Selenium 2.0 and Selenium 3.0?
Selenium 2.0 streamlined creating automated tests for web applications by combining the original Selenium project with the WebDriver project. While Selenium Remote Control (RC) became outdated after the merge, it was still supported for a while to ensure existing tests wouldn't break.
Selenium 2.0Building upon Selenium 2.0, Selenium 3.0 brought a range of improvements. It seamlessly worked with older tests while completely removing reliance on Selenium RC. This new version also squashed bugs and made everything more reliable.
Selenium 3In short, Selenium 3.0 took the strengths of Selenium 2.0, fixed its issues, and made it even better for automating web tests.
8. What are the testing types supported by Selenium?
Selenium is a powerful tool for automated web application testing, supporting various testing types to ensure the quality and functionality of your web applications.
Selenium supports the Regression testing and Functional testing and the following key insight of the same.
Regression Testing: It is the process of Software Testing the modified parts of the code and the parts that might get affected due to the modifications to ensure that no new errors have been introduced in the software after the modifications have been made.
The step involve in the Regression testing which are:
- Identify relevant test cases from existing suites.
- Prioritize test cases based on criticality and priority.
- Execute selected test cases to detect regressions.
- Analyze the test results for failures.
- Report identified defects in bug tracking system.
- Maintain regression test suites as needed.
- Automate regression tests for efficiency.
- Repeat regression testing after each software update.
Functional testing: It is defined as a Type of Testing that verifies that each function of the Software Application works in conformance with the requirement and specification.
The step involve in the Functional testing which are:
- Identify test input: This step involves identifying the functionality that needs to be tested. This can vary from testing the usability functions, and main functions to error conditions.
- Compute expected outcomes: Create input data based on the specifications of the function and determine the output based on these specifications.
- Execute test cases: This step involves executing the designed test cases and recording the output.
- Compare the actual and expected output: In this step, the actual output obtained after executing the test cases is compared with the expected output to determine the amount of deviation in the results.
9. What are the different types of annotations which are used in Selenium?
These are the Types of TestNG Annotations:
- @BeforeSuite - Is executed before the execution of all the test cases inside a TestNG Suite. This annotation allows developers to specify various actions to be taken before the execution of all the test cases inside a TestNG Suite.
- @AfterSuite - This annotation allows developers to specify various actions to be taken after the execution of all the test cases inside a TestNG Suite.
- @BeforeTest - This annotation allows developers to specify various actions to be taken before the execution of all the @test annotated methods inside a TestNG Suite.
- @AfterTest - This annotation allows developers to specify various actions to be taken after the execution of all the @test annotated methods inside a TestNG Suite.
- @BeforeClass - This annotation allows developers to specify various actions to be taken before all the methods of the current class start their execution.
- @AfterClass - This annotation allows developers to specify various actions to be taken after all the methods of the current class finish their execution.
- @BeforeMethod - This annotation allows developers to specify various actions to be taken before test methods are run.
- @AfterMethod - This annotation allows developers to specify various actions to be taken after test methods are run.
- @BeforeGroups - This annotation allows developers to specify various actions to be taken before all the methods of the current group within a class finish their execution.
- @AfterGroups - This annotation allows developers to specify various actions to be taken after all the test methods belonging to a specified group have been run.
10. What are the types of Web locating strategies?
Here are the 7 Locating Strategies:
1. Locating By ID - Id is a locator which is present under the By class. Id locator which is unique for every element in the DOM page that’s why an ID can uniquely identify an element.
element=driver.find_element(By.ID, “element_id”)
2. Locating By Class Name - The class attribute is one of the most commonly used in attribute in HTML .The class attribute allows multiple elements on a webpage to share a common styling. While classes can be shares with other elements , each element class value should be unique within its scope.
element=driver.find_element(By.CLASS_NAME, “element_class_name”)
3. Locating By Name - The name attribute of an HTML element is an identifier like a label . Unlike id ,class the name attribute doesn’t necessarily unique within a web page. They are often used for naming form elements and is used in HTML forms to handle form controls such as input fields , radio button etc.
element=driver.find_element(By.NAME, “element_name”)
4. Locating By Tag Name - Every element in HTML document is identified by a TAG NAME. Tag Name are used to define the purpose and type of an element in a webpage.
element=driver.find_element(By.TAG_NAME, “element_tag_name”)
5. Locating By CSS Selector - CSS Selectors are the patterns that used to select and style HTML elements in web development. CSS selectors allows us to target elements in a web page by their attributes ,position and hierarchy on a web page.
driver.find_element(By.CSS_SELECTOR, “element_css_selector”)
6. Locating By XPath-
XPath (XML Path Language) is a powerful and one of the best locating strategy in Selenium for identifying elements on a web page. XPath is a language used for navigating and querying XML documents but its widely used in web scrapping and automation.
element=driver.find_element(By.XPATH, “element_xpath”)
7. Locating By Link Text And Partial Link Text - Link Texts as the name suggests are primarily used to locate anchor tags ‘<a>’ elements on a webpage . Anchor tags are primarily used for hyperlinks (those which navigate us to different page or resource on a website).
element = driver.find_element(By.LINK_TEXT, “element_link_text”)
11. What are the types of waits supported by WebDriver?
Selenium WebDriver provides three Types of Waits:
Types of Waits- Implicit Waits - An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available.
- Explicit Waits - Explicit wait allows you to wait for a certain condition to be met before proceeding with the next steps in the test script. Unlike implicit wait, direct wait waits for a certain condition to be fulfilled within a certain time This is only used for certain elements where a condition is expected.
- Fluent Wait - Fluent Expectation is an enhancement to Explicit A wait that provides wait conditions over more flexibility and control. It is also known as Fluent Wait. It allows you to specify the maximum time to wait for a condition and the frequency of checking the condition.
12. Mention the types of navigation commands
The Navigation Command provides four methods: to(), back(), forward(), and refresh(). These methods allow the WebDriver to perform the following operations:
- to() Command: Loads a new web page in the current browser window. It accepts a string parameter that specifies the URL of the web page to be loaded.
- back() Command: Moves back one step in the browser’s history stack. It does not accept any parameters and does not return anything.
- forward() Command: Moves forward one step in the browser’s history stack. It does not accept any parameters and does not return anything.
- refresh() Command: Reloads the current web page in the browser window. It does not accept any parameters and does not return anything.
13. What is the Difference Between driver.close() and driver.quit() Commands.
Parameters | driver.close() | driver.quit() |
---|
Execution Scope | Window/Tab-specific | Session-specific |
---|
Effect on WebDriver Session | Doesn’t terminate | Terminates along with windows |
---|
Resource Release | Closes a specific window/tab in focus | Terminates WebDriver and associated windows/tabs |
---|
Effect on Active Window/Tab | Closes the active window/tab | Closes the active window/tab |
---|
Impact on Multiple Windows/Tabs | Doesn’t affect | Closes all windows/tabs |
---|
14. Differentiate Between Manual Testing and Automated Testing.
Here is the Differentiate Between Manual Testing and Automated Testing are as follows:
Parameters | Manual Testing | Automation Testing |
---|
Definition | In manual testing, the test cases are executed by the human tester. | In automated testing, the test cases are executed by the software tools. |
---|
Processing Time | Manual testing is time-consuming. | Automation testing is faster than manual testing. |
---|
Resources requirement | Manual testing takes up human resources. | Automation testing takes up automation tools and trained employees. |
---|
Exploratory testing | Exploratory testing is possible in manual testing. | Exploratory testing is not possible in automation testing. |
---|
Framework requirement | Manual testing doesn’t use frameworks. | Automation testing uses frameworks like Data Drive, |
---|
15. What is an Absolute XPath and Relative XPath?
1. Absolute Path
- An XPath Absolute Path is like giving the full address to find a specific location in an XML document.
- It begins from the root (main) node or starts with a '/' symbol.
- To create an absolute path, you list every parent node in the hierarchy until you reach the desired node.
- It's like providing the complete route to get to a particular point in the XML structure.
2. Relative Path
- A relative path in XPath is like giving directions from your current location to a specific place without starting from the very beginning.
- You don't have to describe the whole journey from the starting point; instead, you begin from where you currently are.
- This makes it simpler to find and pinpoint things in an XML document without having to spell out the entire path from the root.
16. What are the limitations of Selenium testing?
- No dedicated support: Selenium is open-source, so it lacks official tech support for user issues.
- Web applications only: Selenium is mainly for web apps. testing mobile or desktop apps requires extra tools like Appium.
- No image testing: Selenium does not support testing images directly.
- No built-in reports or management: It needs tools like TestNG or JUnit for test reporting and management.
- Programming knowledge needed: Some basic programming skills are required to use Selenium effectively.
17. What is the same-origin policy and how is it handled?
The Same Origin Policy is a security feature in browsers that allows a script on one webpage to access content on another webpage only if both have the same origin (same URL scheme, hostname, and port). This policy helps prevent malicious scripts on one page from accessing sensitive data on a different page.
For example, a JavaScript application on google.com can access all pages within Google’s domain (like google.com/login or google.com/mail) but cannot access pages on other domains (like yahoo.com).
To work around this policy, Selenium RC introduced a server that acts as a proxy, making it seem as if Selenium and the web application are from the same origin. This enables Selenium to test applications more flexibly across domains.
- Easy to Use: Selenium is user-friendly as it's developed in JavaScript, making it accessible and versatile.
- Browser Compatibility: It supports testing on major browsers like Firefox, Opera, Chrome, and Safari.
- Language Flexibility: Test scripts can be written in multiple languages like Java, Python, Perl, and PHP.
- Platform Independent: Selenium works on different operating systems, including Windows, Linux, and macOS.
- Third-Party Integration: Selenium integrates with tools like JUnit and TestNG for better test management and reporting.
- Open-Source and Free: Selenium is a portable, open-source tool available at no cost.
- Cross-Platform Support: Compatible with Linux, Unix, macOS, and Windows operating systems.
- Browser Compatibility: Works with popular browsers like Chrome, Opera, Safari, and Internet Explorer.
- Multi-Language Support: Allows scripting in languages like Java, Python, JavaScript, Perl, Ruby, and more.
- Mobile Testing: Supports testing on iPhone, Android, and Blackberry applications.
- Integration with Build Tools: Easily integrates with frameworks like ANT and Maven for code compilation.
- Resource-Efficient: Uses minimal CPU and RAM, making script execution efficient.
20. What is an exception test in Selenium?
In Selenium, an exception test is used when you expect a specific exception to occur during a test. If a test case is designed to throw a particular exception, you can use the @Test
annotation in TestNG and specify the expected exception type in its parameters. This way, the test will pass only if that exception is thrown.
Here's a simple example:
ExceptionTestExample.java
import org.testng.annotations.Test;
public class ExceptionTestExample {
@Test(expectedExceptions = ArithmeticException.class)
public void testDivideByZero() {
int result = 10 / 0; // This line will throw an ArithmeticException
}
}
In this example, the test will pass because an ArithmeticException
is expected when dividing by zero. If no exception or a different exception is thrown, the test will fail.
21. How to wait until a web page has been loaded completely in Selenium?
In Selenium, you can use two main types of waits to ensure elements load before interacting with them:
- Implicit Wait: Sets a basic wait time for all elements. Selenium will wait for this time before throwing an error if an element isn’t found.
- Explicit Wait: Waits for a specific element or condition to be met, like waiting for a button to appear. This type of wait is more precise.
These waits help manage delays in loading, making tests more reliable.
22. What is Selenium WebDriver?
Selenium WebDriver is a robust open-source framework for automating web browsers, primarily aimed at easing the testing and verification of web applications. As an important part of the Selenium suite, WebDriver offers a programming interface to interact with web browsers, allowing developers and testers to automate browser actions seamlessly.
23. Is Selenium WebDriver a library?
Selenium WebDriver is not a library.
24. Which browsers/drivers are supported by Selenium Webdriver?
Selenium WebDriver works with several popular browsers, including:
This makes it flexible for testing across different browser environments.
25. Explain Selenium 4 and why it is different from other Selenium versions?
Selenium 4 is the latest version of the Selenium testing tool, which bringing new features and improvements over older versions. It offers better support for modern web testing needs, with updated browser compatibility, enhanced debugging, and user-friendly updates for testers.
26. What will happen if I execute this command? driver.get
This command tells the Selenium WebDriver to open the given URL in the web browser.
27. What is an alternative option to driver.get() method to open an URL in Selenium Web Driver?
Another way to open a URL in Selenium WebDriver is by using the driver.navigate().to()
method instead of driver.get()
.
28. Is it possible to test APIs or web services using Selenium Webdriver?
No, Selenium WebDriver cannot be used to test APIs or web services because it is specifically designed for testing web-based applications.
29. Mention different ways of locating an element in Selenium?
The various ways of locating an element in Selenium are: by ID, by name, by classname, by tagname, by link text, by partial link text, by CSS selector, and by XPath.
30. How can we move to the nth-child element using XPath?
You can select the nth-child element using XPath with the following expression: (//parent-element/*)[n]
.
Related Testing Interview Guides
Explore this section to get a hand on Selenium Interview Questions for Intermediate.
31. How to type text in an input box using Selenium?
1. Clicking a Link by Full Text:
driver.findElement(By.linkText("Menu")).click();
This command locates a link on the page with the exact text "Today’s deals" and clicks it, redirecting the user to the linked page.
2. Clicking a Link by Partial Text:
driver.findElement(By.partialLinkText("marks")).click();
Here, the command finds a link that contains the word "Service" in its text (even if it's part of a longer text) and clicks it.
Another Example Using Partial Link Text:
driver.findElement(By.partialLinkText("price")).click();
This would locate and click on a link containing "Discount," even if the full text of the link is "Best Discounts Today."
32. How to scroll down a page using JavaScript?
Purpose of scrollBy()
: The scrollBy()
method allows you to scroll a webpage by a specified number of pixels.
General Syntax:
executeScript("window.scrollBy(x-pixels, y-pixels)");
x-pixels
: Horizontal scroll amount (0 if you don’t want horizontal scrolling).y-pixels
: Vertical scroll amount (positive values scroll down, negative values scroll up).
Example Code:
First, create a JavaScript executor object:
JavascriptExecutor js = (JavascriptExecutor) driver;
Open the desired webpage:
driver.get("https://www.google.com");
Scroll down the page by 1000 pixels:
js.executeScript("window.scrollBy(0, 1000)");
This command scrolls the page vertically down by 1000 pixels.
33. How to click on a hyperlink in Selenium?
Using linkText
:
driver.findElement(By.linkText("Contact Us")).click();
This command searches for a link on the webpage that says "Contact Us." When it finds the link, it clicks on it, and the user is taken to the Contact Us page where they can find information to get in touch with support.
Using partialLinkText
:
driver.findElement(By.partialLinkText("About")).click();
This command looks for any link that has the word "About" in its text. It could be something like "About Us" or "About Our Company." When it finds a link containing "About," it clicks on it, redirecting the user to the About page for more information about the company
34. How to assert the title of a webpage?
Open the GeeksforGeeks website, get the title, and compare it with the expected title.
Java
// Step 1: Open the GeeksforGeeks website
driver.get("https://www.geeksforgeeks.org/");
// Step 2: Get the title of the webpage and store it in a variable
String actualTitle = driver.getTitle();
// Step 3: Set the expected title
String expectedTitle = "GeeksforGeeks - A Computer Science Portal for Geeks";
// Step 4: Check if the titles match
if(actualTitle.equalsIgnoreCase(expectedTitle)) {
System.out.println("Title Matched");
} else {
System.out.println("Title didn't match");
}
// Alternatively, using Assert
Assert.assertEquals(actualTitle, expectedTitle);
35. How to mouse hover over a web element?
Java
// Create an instance of the Actions class
Actions action = new Actions(driver);
// Hover over the web element
action.moveToElement(driver.findElement(By.id("id_of_element"))).perform();
- Actions Class: Create an instance of the Actions class to handle complex mouse and keyboard actions.
- moveToElement(): Use this method to specify the web element you want to hover over, identified by its ID.
- perform(): Call this method to execute the hover action.
This will perform moving the mouse pointer over the specified element.
36. How to retrieve CSS properties of an element?
To retrieve CSS properties of an element in Selenium WebDriver, you can use the getCssValue()
method. Here’s a simple step-by-step method:
- Find the Element: Use a locator to identify the web element (e.g.,
By.id()
, By.className()
). - Use
getCssValue()
: Call the getCssValue("property-name")
method on the found element to get the value of the desired CSS property.
// Retrieve the background color of an element with ID "header"
String backgroundColor = driver.findElement(By.id("header")).getCssValue("background-color");
This code finds the element with ID "header" and retrieves its background color. You can replace "background-color"
with any other CSS property name to get its value.
37. What is POM?
POM is a design pattern which is commonly used in Selenium for Automating the Test Cases. This design pattern can be used with any kind of framework like keyword-driven, Data-driven, hybrid framework, etc.
38. Can Captcha be automated?
No, Selenium cannot automate Captcha. Captcha is designed to prevent bots from accessing sensitive information. Since Captcha requires human input to verify that a user is not a bot, an automation test engineer must manually enter the Captcha while Selenium can fill in the other fields automatically.
39. How does Selenium handle Windows-based pop-ups
Selenium is mainly used for testing web applications and doesn’t support Windows features on its own. However, you can use third-party tools like AutoIT or Robot to work with pop-ups and other Windows-based
40. How to take screenshots in WebDriver?
Step 1. Take a screenshot and store it in a file format
Step 2. Copy screenshot to a location using CopyFile method
FileUtils.copyFile(File, new File(“location where you want to save the image” +FileName+ “.jpeg”));
Step 3. Create a border around the element: Using the JavaScript executor, we can create a border around the desired element.
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(“arguments[0].style.border = ‘3px solid red'”, Element);
41. Why do testers choose Selenium over QTP?
Selenium is a free and open-source tool for automated testing, while QTP (Quick Test Professional) is a paid tool that requires a significant investment. Selenium works with many programming languages and can run on different operating systems.
42. What are the data-driven framework and keyword-driven framework?
A data-driven framework is a type of test automation framework that keeps the test data separate from the test scripts. This means testers can create automated tests that can work with different sets of data without changing the test code itself.
43. What is the difference between getwindowhandles() and getwindowhandle()?
The `getWindowHandle()` method gives you a unique ID for the current browser window. In contrast, the `getWindowHandles()` method returns a list of unique IDs for all the browser windows that WebDriver has opened.
44. What is a Selenium Maven project?
A Selenium Maven project is a software project that uses Maven to handle the project's dependencies (like libraries needed for testing) and to manage the build process (how the project is compiled and packaged). This makes it easier to organize and run Selenium tests efficiently.
45. What is an Object Repository?
An Object Repository is a central place where testers keep all the web elements used in automated tests, like buttons, text boxes, and links. This makes it easier to manage and use these elements in the testing framework, allowing for better organization and reusability in test scripts.
46. How do you Locate Elements using XPath?
1. Absolute XPath: Absolute XPath uses the root element of the HTML/XML code and is followed by all the elements that are necessary to reach the desired element. It starts with the forward slash ‘/’. Generally, Absolute XPath is not recommended because in the future any of the web elements when added or removed then Absolute XPath changes.
Example:
/html[1]/body[1]/div[6]/div[1]/div[3]/div[1]/div[1]/div[1]/div[3]/ul[1]/li[2]/a[1]

2. Relative XPath; In this, XPath begins with the double forward slash ‘//’ which means it can search the element anywhere on the web page. Generally, Relative Xpath is preferred as they are not complete paths from the Root node.
Example:
//input[@id = 'fakebox-input']

47. How can you Handle Multiple Windows in Selenium?
Sometimes when we click on a particular web element it opens a new window. To locate the web elements on the new window webpage, we need to shift the focus of selenium from the current page (main page) to the new page. We will try to shift the focus of selenium from one window to another new window. Here we will use the Chrome browser for which we require ChromeDriver you can download it from the official site of Selenium.
To get the IDs of different windows
Java
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GFGIDsOfWindows {
public static void main(String[] args) throws InterruptedException {
// Set the path for the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
// Create an instance of ChromeDriver
WebDriver driver = new ChromeDriver();
// Open the GeeksforGeeks website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the browser window
driver.manage().window().maximize();
// Delete all cookies
driver.manage().deleteAllCookies();
// Scroll down to locate an element
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0, 200)");
// Click on the 'Courses at GeeksforGeeks' link
driver.findElement(By.xpath("(//span[text()='Courses at GeeksforGeeks'])[2]")).click();
// Wait for a moment to allow the new page to load
Thread.sleep(2000);
// Click on a specific course (Data Structures and Algorithms)
driver.findElement(By.xpath("(//h4[text()='Data Structures and Algorithms - Self Paced'])[1]")).click();
// Get the ID of the main (parent) window
String parentId = driver.getWindowHandle();
System.out.println("Parent Window ID: " + parentId);
// Get the IDs of all open windows (child windows)
Set<String> windowIds = driver.getWindowHandles();
// Print the IDs of all windows
for (String id : windowIds) {
System.out.println("Window ID: " + id);
}
// Optionally, you can switch back to the parent window if needed
driver.switchTo().window(parentId);
// Close the browser
driver.quit();
}
}
Here you can observe the IDs of windows are different.
CDwindow-EA925E71098EEFBB80858BE787CED1A5 (ID of main window)
CDwindow-C9078346729F1D0CF8AF12E938CE49DD (ID of new window)
Output Video
48. Explain Implicit and Explicit waits in Selenium.
Implicit Cursors | Explicit Cursors |
---|
Implicit cursors are automatically created when select statements are executed. | Explicit cursors need to be defined explicitly by the user by providing a name. |
They are capable of fetching a single row at a time. | Explicit cursors can fetch multiple rows. |
Closes automatically after execution. | Need to close after execution. |
They are more vulnerable to errors such as Data errors, etc. | They are less vulnerable to errors(Data errors etc.) |
Provides less programmatic control to the users | The user/Programmer has the entire control. |
49. List the Difference Between Close and Quit Commands.
Parameters | driver.close() | driver.quit() |
---|
Execution Scope | Window/Tab-specific | Session-specific |
---|
Effect on WebDriver Session | Doesn’t terminate | Terminates along with windows |
---|
Resource Release | Closes a specific window/tab in focus | Terminates WebDriver and associated windows/tabs |
---|
Effect on Active Window/Tab | Closes the active window/tab | Closes the active window/tab |
---|
Impact on Multiple Windows/Tabs | Doesn’t affect | Closes all windows/tabs |
---|
These is how We can submit a form in Selenium:
1. Create a Form
We will create the Form which we will be using to try submitting it from the Selenium script.
2. Code to Submit a Form
we will try to get the fields in which all the data will be entered. To get the field we can find it using Id, Class, or CSS Selector. In the following code blocks, we will find the fields using ID and the submit button will be tracked using the CSS Selector.
Java
// Import necessary libraries
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) {
// Set the path to the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
// Initialize WebDriver
WebDriver driver = new ChromeDriver();
// Navigate to the desired webpage
driver.get("your-webpage-url"); // Replace with your actual webpage URL
// Find the input fields using ID
WebElement firstName = driver.findElement(By.id("FirstName"));
WebElement dob = driver.findElement(By.id("DOB"));
WebElement email = driver.findElement(By.id("Email"));
WebElement inputNumber = driver.findElement(By.id("inputNumber"));
WebElement maleRadioButton = driver.findElement(By.id("Male"));
// Fill out the form
firstName.sendKeys("Ram");
dob.sendKeys("19-01-2001");
email.sendKeys("[email protected]");
inputNumber.sendKeys("1234567890");
maleRadioButton.click();
// Click the submit button using CSS Selector
driver.findElement(By.cssSelector("button[type='submit']")).click();
// Switch to alert and get alert text
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
// Check if the alert is open
if (alertText != null) {
System.out.println("Alert is open, and the content is: " + alertText);
// Optionally accept the alert if needed
// alert.accept();
} else {
System.out.println("No alert found.");
}
// Quit the browser
driver.quit();
}
}
Output:
Below is the output of the code in which the Chrome browser loads and is redirected to the form. And quickly fills in the details and submits the form. We can see that the alert box opens up and the details are shown in the console window.
.gif)
51. How to delete cookies in Selenium?
Method 1: Using the deleteAllCookies()
command:
Python:
driver.manage().deleteAllCookies()
Java:
driver.manage().deleteAllCookies()
JavaScript:
await driver.manage().deleteAllCookies()
Method 2: Going to the browser's settings:
Opening your browser's settings menu and manually emptying the cookie jar.
Steps:
- Open the settings page: Type
driver.get("chrome://settings/clearBrowserData")
- Find the "Clear Data" button: Use your browser's developer tools to pinpoint it (like a treasure map!).
- Click the button: This will make all the cookies disappear.
52. How do you work with frames in Selenium?
1. Finding the Frame:
- Right-click check: Right-click on the part of the page you think is a frame. If you see options like "This Frame," "View Frame Source," or "Reload Frame," it's a frame!
- Code search: Look for
<iframe>
tags in the website's code to find all the frames.
2. Stepping Inside the Frame:
- Use a special code: Use
driver.switchTo().frame()
to step inside the frame. You can tell it which frame you want by:- Its order on the page (like first, second, etc.)
- Its name or ID
- Finding it like any other element on the page
3. Do Your Thing:
- Once you're inside, you can work with elements inside the frame just like you would normally.
4. Stepping Back Out:
- When you're done, use
driver.switchTo().defaultContent()
to step back out to the main page.
For more refer -Frames in Selenium
53. Discuss the importance of Cross-Browser Testing in Selenium.
Ensures Consistent User Experience:
- Different browsers use varying rendering engines, leading to potential inconsistencies in website appearance and functionality. Cross-browser testing with Selenium helps identify and fix these issues, ensuring a smooth and consistent user experience across all major browsers.
Reaches Wider Audience:
- By testing on multiple browsers, you cater to a larger audience who use diverse platforms and devices. This expands your reach and avoids alienating users due to browser-specific compatibility issues.
Improves Quality and Reduces Risk:
- Early detection and fixing of cross-browser issues prevent bugs and glitches from reaching production, enhancing overall website quality and reducing the risk of user frustration and potential financial losses.
Boosts Search Engine Optimization (SEO):
- Search engines consider website compatibility across different browsers as a ranking factor. By ensuring flawless cross-browser performance, you improve your website's SEO and visibility in search results.
Benefits of using Selenium for Cross-Browser Testing:
- Automation: Selenium automates repetitive testing tasks across various browsers, saving time and effort compared to manual testing.
- Flexibility: It supports multiple programming languages and integrates with various testing frameworks, offering flexibility for development teams.
- Open-Source: Its open-source nature makes it cost-effective and accessible to developers of all levels.
For more refer -Cross-Browser Testing
54. What are the challenges of Parallel Execution in Selenium?
Parallel execution in Selenium brings many benefits, like faster test execution and improved test coverage, but it also comes with its own set of challenges:
Test Dependencies:
- Data isolation: Tests that share data or rely on specific states can interfere with each other when running in parallel. Careful test design and data management are crucial.
- Test order dependencies: Some tests might need to run in a specific order due to logical dependencies. Parallel execution might disrupt this order.
Resource Management:
- WebDriver limitations: Not all WebDrivers support parallel execution. For example, EdgeDriver requires special handling.
- Resource contention: Running multiple browser instances simultaneously can strain your machine's resources (CPU, memory, network). Consider using grid-based solutions or cloud services for larger-scale testing.
- Test stability: Parallelization can introduce flakiness in tests due to unforeseen interactions between tests.
Other Challenges:
- Synchronization issues: Tests might access shared resources, leading to race conditions and unpredictable behavior. Proper synchronization mechanisms are needed.
- Debugging difficulties: Identifying the root cause of failures in parallel execution can be complex due to multiple test interactions. Logging and reporting tools are helpful.
- Test environment setup: Setting up and managing multiple test environments for parallel execution can be complex and time-consuming.
55. How to integrate Selenium with Jenkins for Continuous Integration?
1. Setting Up Jenkins:
- Install necessary plugins: Install plugins like "Pipeline", "Git", and any relevant browser-specific plugins depending on your needs (e.g., BrowserStack Plugin).
- Configure JDK: Define a Java installation for running your Selenium tests.
2. Preparing your Selenium Tests:
- Organize your tests: Structure your tests into well-defined modules or classes depending on your testing framework.
- Manage dependencies: Ensure your test scripts have access to required libraries and resources.
- Consider using a testing framework: Frameworks like TestNG or JUnit help manage dependencies and parallel execution.
3. Creating a Jenkins Job:
- Choose a job type: Select "Pipeline" for declarative builds or "Freestyle Project" for a more traditional configuration.
- Define job triggers: Choose how you want the job to run (e.g., manually, upon code changes, scheduled builds).
- Configure build steps:
- Download code: Use the "Git Plugin" to fetch code from your version control system.
- Install dependencies: Download and install required libraries for your tests (e.g., Maven build step).
- Execute tests: Use a "Shell" or "Execute Windows batch command" step to run your Selenium tests.
- Use environment variables to specify browser configurations and other test parameters.
- Capture test results and logs for reporting.
Publish reports: Utilize plugins like "JUnit" or "XUnit" to publish test results in Jenkins dashboard.
4. Additional Considerations:
- Parallel execution: Utilize tools like Selenium Grid or cloud testing services for efficient parallel execution.
- Security: Securely store credentials and sensitive information using Jenkins credentials management.
- Reporting and analysis: Integrate reporting tools and dashboards for better visualization and analysis of test results.
56. What is the purpose of the Page Factory in Selenium?
1. Initialization:
- It eliminates the need for manually finding web elements using
find_element
or find_elements
methods in every test method. Instead, you use annotations like @FindBy
to declare and locate elements within your page object classes. - Page Factory then automatically initializes these elements during the first interaction with the page object, reducing boilerplate code and improving readability.
2. Improved Readability:
- By separating element declaration and logic within page objects, your test scripts become more focused and easier to understand. You can clearly see which actions belong to each page and how they interact with the elements.
3. Maintainability:
- If the structure of a web page changes, you only need to update the element locators within the page object class, keeping your test scripts independent of UI changes. This helps maintain your automation suite as your application evolves.
4. Reusability:
- Page objects can be reused across different test cases, promoting code reuse and reducing redundancy.
5. Additional Features:
- Some Page Factory implementations offer additional functionalities like custom waits, implicit waits, and error handling, further enhancing your testing experience.
Overall, Page Factory plays a crucial role in making your Selenium tests more modular, readable, maintainable, and reusable. It streamlines the implementation of the POM and promotes efficient test automation practices.
57. Describe the Significance of TestNG in Selenium.
Enhanced Organization and Readability:
- Annotations: TestNG relies heavily on annotations like
@Test
, @BeforeTest
, and @AfterTest
to clearly define test methods, setup/teardown procedures, and group related tests. This improves code organization and readability compared to traditional methods. - Grouping: You can group tests based on functionality, criticality, or any other relevant criteria for organized execution and reporting.
Powerful Features for Efficient Testing:
- Data-Driven Testing: TestNG excels at data-driven testing, allowing you to parameterize test data and execute the same test with different sets of data using the
@DataProvider
annotation. This streamlines repetitive tests and increases test coverage. - Parallel Testing: TestNG allows parallel execution of tests across multiple threads or machines, significantly reducing test execution time, especially for large test suites. This is crucial for modern web applications with complex functionalities.
- Dependencies and Sequencing: You can control the order of test execution and define dependencies between tests using annotations like
@DependsOnMethods
. This ensures tests run in a specific order and only when their prerequisites are met.
Improved Reporting and Debugging:
- Detailed Reports: TestNG generates comprehensive HTML reports with detailed information about each test, including status, execution time, and stack traces for failures. This aids in analyzing test results and identifying issues efficiently.
- Logging: TestNG integrates seamlessly with logging frameworks like Log4j, allowing you to log test events and debug issues more effectively.
Beyond these core features, TestNG brings additional benefits:
- Flexibility: It supports various programming languages and integrates well with other testing tools and frameworks.
- Open-Source: Being open-source, it's accessible and free to use, making it a popular choice for many projects.
In conclusion, TestNG's rich feature set and focus on efficient test execution make it an ideal choice for Selenium automation. It helps you write more organized, maintainable, and data-driven tests while offering valuable tools for reporting and debugging.
58. How do you handle Synchronization in Selenium WebDriver?
1. Implicit Waits:
- Like setting a general timeout: Selenium will patiently wait a certain amount of time for elements to appear before giving up.
- Good for simple tasks: It's easy to use, but it might waste time if the website is usually fast.
2. Explicit Waits:
- Like checking for specific signs: Selenium looks for specific conditions, like a certain button being clickable, before proceeding.
- More efficient and focused: It doesn't waste time waiting unnecessarily, but it requires more instructions.
3. Fluent Waits:
- Like a customizable waiting game: Selenium can adjust its waiting strategy based on how the website behaves, making it even more adaptable.
- Very flexible but can be tricky: It offers a lot of control, but it takes more effort to set up correctly.
4. Thread.sleep():
- Like taking a nap: Selenium just pauses for a fixed time, regardless of what's happening on the website.
- Not a good idea: It can cause unreliability and slow down tests, so it's generally avoided.
Remember, choosing the right waiting strategy helps Selenium work smoothly and accurately, ensuring your tests run reliably and efficiently!
Here's why using Selenium Grid is a good idea:
- Speeding things Up: Imagine running your cleaning, cooking, and laundry all at once! Grid lets you run your tests on different browsers and devices at the same time, making them much faster, especially for long test lists.
- Testing Everywhere: Just like you wouldn't clean only one corner of your house, Grid lets you test your website on different browsers, operating systems, and even real devices like phones and tablets. This ensures your website works smoothly for everyone, no matter what they use.
- Sharing is Caring: Instead of each helper needing their own cleaning supplies, Grid shares resources efficiently. This means less waste and better use of your testing machines and browsers.
- Growing with you: Need more helpers for a big spring cleaning? Grid lets you easily add or remove testing machines as needed, making it flexible and adaptable to your testing needs.
- Saving Money: Buying lots of cleaning supplies can be expensive! Grid lets you use cloud services to test on real devices without actually buying them, saving you money.
- Building Something Awesome: Just like a clean and well-maintained house makes you feel good, Grid helps you catch problems in your website before they affect real users, leading to a more reliable and high-quality website.
60. Explain the Purpose of the 'assert' statement in Selenium.
Imagine Selenium as a friendly robot that's testing your website for you. It's like a teacher checking your homework, but for websites!
Here's how Selenium uses assert
statements to make sure your website is working correctly:
- Selenium does something on the website: Like a student solving a math problem, Selenium interacts with the website, such as clicking buttons, filling forms, or reading text.
- Selenium expects a certain result: Just like a teacher has the answer key, you tell Selenium what the correct outcome should be after each action, like a certain message appearing or a page opening.
- Selenium double-checks with
assert
: The assert
statement is like Selenium raising its hand to say "I'm done!" It compares the actual result it got with the expected outcome you provided. - Test passes or fails: If everything matches, the test passes, like getting a good grade on a test! If not, the test fails, meaning something might be wrong with the website.
For more refer -'assert' statement in Selenium
Advanced-Level Selenium Interview Questions
61. What are the limitations of Selenium testing?
With concerning all these advantages of Selenium include some Limitation of selenium. which are as follows:
- Cross Browser Compatibility: Selenium can give regular best results across multiple browsers, but sometimes it's restricted in that the web browsers understand and use the HTML and CSS differently from the respective browsers.
- Slow Test Execution: Because the automation depends on the various drivers with the browser that causes the process to slow. Selenium will be slow to respond when running tests on big web applications or websites
- Difficulty in Handling Dynamic Web Elements: Selenium has difficulties in interacting with dynamic web elements like ID that will change on a web page sometime which causes the test script failure while testing the same.
- Limited Support for Mobile Applications: Selenium will not provide the automation on the mobile application testing, so developers choose the other tools or frameworks for the automation purpose.
- Limited Support for Windows-based Applications: Developers will have to depend on the third-party tools or libraries for Automation testing in desktop apps using Selenium.
62. Differentiate between WebDriver and WebElement.
Feature | WebDriver | WebElement |
---|
Purpose | Controls the browser itself | Represents an element within a web page |
---|
Scope | Browser window or tab | Specific element like a button, text field, or image |
---|
Interactions | Opens/closes browsers, navigates to URLs, refreshes pages, manages windows | Clicks, enters text, retrieves text, submits forms, interacts with element attributes |
---|
Example | driver.get("https://www.example.com") | element = driver.find_element_by_id("my_button") |
---|
Analogy | Like a remote control for your TV | Like a specific button or channel on your Television |
---|
Selenium Methods | get() , navigate() , quit() , switch_to() | click() , send_keys() , text() , get_attribute() , is_displayed() |
---|
Selenium can't peek directly into your website's database, but we can be clever detectives! Here are some ways to check the database indirectly:
- Action, reaction, verification:
- Use Selenium to perform actions on the website (like adding an item to a cart).
- Peek into the database afterwards using separate tools or queries to see if the expected changes happened. Imagine checking your bank balance after a purchase
- Mocking the database:
- Think of this as setting up a fake database with controlled responses.
- Tools like Mockito help create this "pretend database" that responds to Selenium's actions the way the real one should.
- This lets you test how your website interacts with the database without going near the real one.
- Speaking the database language:
- Libraries like JDBCTest allow you to talk directly to the database, but it's like learning a new language!
- This gives you fine-grained control, but be prepared to put in some extra effort.
- Testing frameworks lend a hand:
- Imagine Robot Framework or Cypress as assistants who already know how to talk to databases.
- They offer shortcuts and modules to make database testing smoother within your Selenium tests.
64. Discuss the Role of the Cucumber Framework in Selenium.
Imagine trying to explain test scripts to your boss who doesn't know code? That's where Selenium and Cucumber come in, working together like a dream team for web testing!
Selenium: Think of it as a robot ninja, flawlessly controlling the browser and doing all the clicking, typing, and navigating. But for most people, its code can be like hieroglyphics!
Cucumber: Enter the superhero translator, Cucumber, who speaks plain English! It uses Gherkin, a special language that reads like a story, describing how users interact with the website. This makes tests clear for everyone, not just coding experts.
Together, they're unstoppable:
- Communication Boost: With Cucumber, everyone (testers, developers, even your boss!) can understand the test scenarios. No more confusion, just clear expectations!
- Focus on What Matters: Forget the code details, Cucumber lets you focus on how the website should behave from a user's perspective. Easier to write, easier to understand, easier to maintain!
- Living Documentation: The Gherkin scenarios double as documentation, always showing what the website should do. No more outdated docs, just clear instructions for everyone!
- Flexibility: Whether you prefer Behavior-Driven Development (BDD) or Test-Driven Development (TDD), Cucumber works with both, adapting to your team's style.
65. What are the Considerations for Headless Browser Testing in Selenium?
Headless browser testing with Selenium sounds amazing - imagine tests running super fast without needing all those open browser windows! But before you jump in, let's talk about what to watch out for:
The Good Stuff:
- Lightning Speed: No fancy graphics means tests fly through, especially for long lists. Need to test on 10 browsers? Headless makes it a breeze!
- Scale Up, Save Big: Run tests on multiple machines or browsers at once, even without fancy displays. This saves resources and lets you test more efficiently.
- Automation Rockstar: Want to automatically take screenshots or measure website performance? Headless can handle it, freeing you up for other tasks.
- Cost Cutter: Testing mobile websites often requires actual devices, which can be expensive. Headless lets you use virtual versions, saving you money.
The Not-So-Good Stuff:
- Seeing is Believing: Headless can miss visual details like layout, animations, or hover effects. These might still need separate testing.
- Debugging Detective: Fixing problems can be trickier without seeing what's happening on the screen. Think of it like solving a crime scene in the dark!
- JavaScript Jitters: Some complex JavaScript interactions or libraries might not work quite right in headless mode. Test carefully!
- Not Everyone's Invited: Older browsers or specific versions might not play nice with headless testing. Make sure everyone's compatible.
- Security Matters: Running tests on cloud platforms? Double-check your security measures to keep things safe.
66. Examine the Challenges and Solutions of Handling Dynamic Tables.
Imagine a web page with a table that keeps changing its data, rows, or even structure. Testing that with Selenium can feel like fighting a shapeshifting monster! But worry not, we have tools and strategies to overlap these Gap.
The Challenges:
- Finding the right element: Tables rarely have unique IDs or names, making them hard to pinpoint. It's like trying to find a specific grain of sand on a beach!
- Paging through data: If information spills across multiple pages, navigating and testing each one adds complexity. Think climbing Mount Everest, one page at a time!
- Sorting and filtering chaos: Sorting and filtering can scramble the table's layout, breaking your identification methods. It's like the table rearranging itself just to confuse you!
- Extracting the right info: Grabbing specific data within the table can be tricky. It's like finding a hidden treasure in a maze!
The Solutions:
- Think like a detective: Use relative locators like CSS selectors or XPath that rely on the table's structure and relationships between elements. Think Sherlock Holmes, using clues to identify the culprit!
- Plan your attack: Prepare test data beforehand and compare it to the actual table data to see if everything matches. It's like having a blueprint of the treasure chest to know what you're looking for!
- Wait patiently: Use explicit waits like
WebDriverWait
until specific elements appear or conditions are met before interacting. It's like waiting for the right moment to strike, not rushing in blindly! - Javascript to the rescue: Sometimes, using Javascript within your tests can manipulate the table or directly extract data. It's like having a secret weapon to unlock the treasure chest!
- Specialized tools: Consider libraries like
robotframework-table
or TableAU
designed specifically for handling these dynamic tables. They're like special tools Indiana Jones would use to navigate ancient tombs! - Frame it right: If the table is within a frame, switch to the correct frame before interacting with it. It's like entering the right room in a museum to find the specific exhibit!
For more refer - Handling Dynamic Tables
67. How to Capture Screenshots in Selenium?
1. Using the TakesScreenshot Interface:
This is the most basic and widely used approach:
Java
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class ScreenshotExample {
public static void main(String[] args) throws Exception
{
// Set up your WebDriver instance
WebDriver driver = ...;
// Navigate to the URL
driver.get("https://www.example.com");
//screenshot of the entire page
TakesScreenshot screenshot = (TakesScreenshot) driver;
byte[] screenshotBytes = screenshot.getScreenshotAs(OutputType.BYTES);
// Save the screenshot to a file
FileUtils.writeByteArrayToFile(new File("screenshot.png"), screenshotBytes);
// Quit
driver.quit();
}
}
2. Using Third-Party Libraries:
Libraries like Apache Commons IO and SikuliX offer additional functionalities:
- Apache Commons IO: Provides efficient byte array handling for saving screenshots.
- SikuliX: Captures screenshots of specific screen regions based on visual elements.
3. Browser Developer Tools:
While not ideal for automated tests, browser developer tools allow manual screenshot capture during debugging.
For more refer - Capture Screenshots in Selenium.
68. What are the Benefits of Using a Data-Driven Framework in Selenium?
Benefits:
- One-Time Setup, Endless Possibilities: Write your test script once, then feed it different ingredients (data sets) to create countless flavor combinations (test cases). No more repetitive recipe writing!
- Taste Everything on the Menu: Effortlessly test different data combinations to uncover hidden flavors (bugs) and ensure every dish is perfect (broader test coverage).
- Easy Recipe Adjustments: When you need to change an ingredient (update test data), simply edit the recipe book (data file), not the whole kitchen (test script).
- Clear Instructions for Everyone: Separate ingredients (data) from cooking instructions (test logic) so anyone can follow the recipe (understand the test).
- Share Your Secret Sauce: Shareable recipe books (data sets) and reusable cooking techniques (test logic) mean your team can collaborate and cook faster together.
How it Works:
- Stock Your Pantry: Store your ingredients (test data) in separate containers like Excel spreadsheets, CSV files, or databases.
- Follow the Recipe: Your test script reads the recipe book (data), grabs the right ingredients, and uses them to cook the dish (perform actions on the website).
- Taste Test: Compare the final dish (actual results) with the expected taste (expected values) from the recipe book (data).
69. Explain the concept of Object Repository in Selenium.
Imagine you're a detective testing a website for clues. Memorizing the location of every door, window, and secret passage would be tough, right? That's where an object repository in Selenium comes in!
Think of it like a map:
- Each web element (door, window) has a unique address (locator) stored in the map (repository).
- Instead of remembering every address, you simply consult the map to find what you need.
Benefits of using this map:
- Easy updates: Website changes? No problem! Update the address on the map (repository), and your tests automatically find the right element.
- Reuse the map: Need to check the same window in different tests? No need to draw a new map, just reuse the existing one (share locators between tests).
- Clear instructions: Separate the addresses (locators) from your detective work (test logic), making your notes (scripts) easier to understand for everyone.
- Be flexible: Use different search methods (locator strategies) to find elements, choosing the most reliable one for each situation.
1. Using the Actions class:
This is the most common and built-in method. Here's how it works:
Java
// Import necessary libraries
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class HoverAndClickExample {
public static void main(String[] args) {
// Set up your WebDriver instance (e.g., ChromeDriver)
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
WebDriver driver = new ChromeDriver();
try {
// Navigate to the desired webpage
driver.get("https://www.example.com"); // Replace with your target URL
// Find the element to hover over
WebElement elementToHover = driver.findElement(By.id("hoverElement")); // Replace with the actual ID
// Create an Actions object
Actions actions = new Actions(driver);
// Move the mouse over the element
actions.moveToElement(elementToHover).perform();
// Wait for sub-menu to be visible (if necessary)
Thread.sleep(1000); // Adjust the time as needed
// Click on a sub-menu item
actions.click(driver.findElement(By.xpath("//li[text()='SubMenu Item']"))).perform(); // Replace with actual submenu item
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Quit the browser
driver.quit();
}
}
}
2. Using JavaScript:
In specific cases, you might prefer using JavaScript for more fine-grained control:
Java
// Import necessary libraries
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HoverExample {
public static void main(String[] args) {
// Set up your WebDriver instance (e.g., ChromeDriver)
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
WebDriver driver = new ChromeDriver();
try {
// Navigate to the desired webpage
driver.get("https://www.example.com"); // Replace with your target URL
// Find the element to hover over
WebElement elementToHover = driver.findElement(By.id("hoverElement")); // Replace with the actual ID
// Cast the WebDriver to JavascriptExecutor
JavascriptExecutor js = (JavascriptExecutor) driver;
// Execute JavaScript to hover over the element
js.executeScript("arguments[0].dispatchEvent(new MouseEvent('mouseover'));", elementToHover);
// Optionally, you can perform another action after hovering
// For example, clicking another element after hovering
// WebElement anotherElement = driver.findElement(By.id("anotherElementId"));
// anotherElement.click();
} finally {
// Quit the browser
driver.quit();
}
}
}
For more refer - Mouse Hover Actions in Selenium.
Conclusion
As in the end we know the Selenium concepts not only focus on the Theoretical Concepts is also focused on the Practical Based Questions which is really helpful for improving the Knowledge of the Automation Testing.
Similar Reads
Galaxe Solutions Interview Questions
Below is the list of Questions asked in Galaxe Solutions technical interview for a java full stack developer: 1. What is ActiveMQ and JMS? 2. How to process multiple messages in the queue? 3. What are the configurations required in ActiveMQ? 4. In a class we have three different methods with same na
3 min read
GE India Interview Questions
Below is the list of Questions asked in GE interview for a java developer: 1. How to find the 3rd element from end in linked list in Java (Java Program to find the Nth Node from tail in linked list) 2. Write producer consumer problem? 3. wait, notify and notifyall whose methods? 4. Why wait, notify
2 min read
AllState Interview Questions
Below are few questions asked in AllState technical round for a JAVA developer: 1. How hash set works? 2. What happens happens when we iterate over concurrent hashmap and hashmap? 3. Difference between concurrent hashmap and hashmap? 4. Why to use concurrent hashmap? 5. Write a program to find the i
2 min read
Standard Chartered Interview Questions
Below is the list of Questions asked in Standard Chartered 1st round of technical F2F interview for a java developer: 1. [1, 0, 1, 0, 0, 0, 1, 1, 1] -> What will be the suitable algorithms used to sort this array, Can we use threads here? 2. [1, 2, 3, 4, 3, 2, 1, 4, 3, 3, 3] -> What will be th
1 min read
Aricent Interview | Set 1 (On-Campus)
Aricent(On Campus) Recruitment Process Divided into 3 parts: Round 1: Online Written test(AMCAT). Sections: 1.1 English. 1.2 Aptitude. 1.3 Logical Reasoning. 1.4 Computer Programming. 1.5 Computer Science. Round 2: Technical Interview. Round 3 : HR interview. Round 2 : Technical Interview question:
2 min read
Intuit Interview | Set 1 (On-Campus)
Intuit had come on campus last week for SDE-1 and Quality Engineer profiles. These were the questions asked: Round One (Written round, 60 minutes): 1. Given an unsorted linked list, remove all duplicates without using temporary buffers. 2. A number starting from 1 can be got by either multiplying 3
3 min read
Ixigo Interview Experienced for SDE2 Backend
There were two interviewers. Asked questions primarily around Java 1. What is the difference between two String assignments, via literal and constructor. They wanted to know about the String Constant Pool and its working. 2. What is the Java Memory Model 3. How does the Java code gets compiled and e
1 min read
Unisys Interview Experience | Set 1 (On-Campus)
Unisys visited our campus for a recruitment drive, providing an exciting opportunity for students to showcase their skills and secure positions in their esteemed organization.Round 1: 25 MCQs based on C,C++ and JAVA(concept based) , 25 MCQs based on aptitude, 15 output related MCQs, 25 MCQs on Data
3 min read
Expedia Interview | Set 4 (On-Campus)
My Expedia Experience:- Online Test:- MCQs on almost all topics (DS, Algo, C, C++, OS, DBMS, Quant, Verbal) 2 coding problems:- 1. Rat in a maze(Basic Recursion Problem). 2. Given a undirected graph check whether it is a tree or not(Easy BFS problem). F2F Technical Interview Round 1(40 Mins): -Very
1 min read
Amdocs Interview Experience | Set 3 (On-Campus)
TOTAL 3 ROUNDS: ROUND 1::ONLINE TEST...topics (a) quantative aptitude (b) logical reasoning (c) unix(technical part) (d) sql / pl-sql(technnical part) (e) english comprehension and general questions (f) coding ( 2 easy question in 45 minutes).if u have an average coding skill than its easy for you.
2 min read