Daily 10 Selenium Interview Questions – Part 2

Daily 10 Selenium Interview Questions – Part 2

seleniumInterviewQuestion

Welcome back to our series: "Daily 10 Selenium Interview Questions". We started this journey to make interview prep feel less stressful and more like a friendly chat over chai. 😊 If you missed Part 1, make sure to check it out — we covered some of the basics that every QA professional should know.
Today in Part 2, we’re diving a bit deeper, but we’ll still keep it simple and beginner-friendly. Whether you're prepping for a manual to automation transition or just brushing up for an upcoming interview, you're in the right place.

Let’s get started with today’s 10 questions! 🚀

1. Explain the WebDriver instantiation process.

Instantiating WebDriver means creating an object to control the browser. You usually do it like this:
WebDriver driver = new ChromeDriver();
Let’s break this down:
WebDriver is an interface — a kind of blueprint.
ChromeDriver is a class that implements the WebDriver interface for Chrome.
So, this line of code means: "Create a WebDriver-style object, specifically for Chrome."
ChromeDriver extends RemoteWebDriver, which helps it communicate with the actual browser.

2. What is a Page Object Model (POM)? Difference between POM vs PageFactory in short?

Page Object Model (POM) is a design pattern that helps separate the logic of test scripts from the page details. It improves maintainability and reduces code duplication.
POM: Manually define locators and actions.
PageFactory: Uses annotations like @FindBy to initialize elements automatically, offering better readability.

3. What is a Singleton Design Pattern?

This pattern ensures that only one instance of a class exists at any time.
Example use in Selenium: To reuse the same WebDriver instance across tests.
Example Code:
public class DriverSingleton {
    private static WebDriver driver;                      //This line creates a single, shared WebDriver object. It's marked static so it belongs to the class, not individual objects.
private DriverSingleton() {}                                 //This is a private constructor. It means you can’t create an object of DriverSingleton class from outside. This is key to the Singleton pattern, which ensures only one instance exists.
public static WebDriver getDriver() {                  //This is the main method used to get the WebDriver instance     
  if (driver == null) {
            driver = new ChromeDriver();
        }
        return driver;
    }
}
So every time someone calls DriverSingleton.getDriver(), they get the same driver instance — no duplicates.

4. What are types of exceptions in Selenium? How do you handle them?

In Selenium (and Java in general), exceptions fall into two buckets:

Checked Exceptions (Compile-Time) - These are the ones Java forces you to handle during compilation — like homework that must be done before class!

Example:
IOException (when reading a file like Excel or JSON)
FileNotFoundException

Handling: Use try-catch blocks or declare with throws keyword.
try {
    FileInputStream file = new FileInputStream("data.xlsx");
} catch (FileNotFoundException e) {
    System.out.println("File not found!");
}

Unchecked Exceptions (Run-Time) - These pop up during execution — like surprise guests!

Example (common in Selenium):
NoSuchElementException
TimeoutException
StaleElementReferenceException

Handling: Wrap in try-catch or use smart waits (WebDriverWait) to avoid them.
try {
    driver.findElement(By.id("submit")).click();
} catch (NoSuchElementException e) {
    System.out.println("Element not found!");
}
Pro Tip: Use explicit waits to reduce flaky tests and handle exceptions gracefully to avoid sudden test crashes.

5. Code snippet for Drag and Drop in Selenium

WebElement source = driver.findElement(By.id("source"));
WebElement target = driver.findElement(By.id("target"));
Actions actions = new Actions(driver);
actions.dragAndDrop(source, target).build().perform();

6. How do you switch between multiple windows? Why is Set used with window handles?

String mainWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
for(String window : allWindows){
    if(!window.equals(mainWindow)){
        driver.switchTo().window(window);
    }
}
When a new window or tab opens, Selenium needs to switch focus to it.
We use getWindowHandle() to get the current window, and getWindowHandles() to get all open ones.
A Set is used because window IDs are unique and unordered. This lets you loop through and switch to the new window for further actions — super useful for popups or third-party logins!

7. Code for taking a screenshot in Selenium (Full and Element Specific)

Full Page Screenshot:

TakesScreenshot ts = (TakesScreenshot) driver;
File full = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(full, new File("full.png"));

Element Screenshot (Selenium 4):

WebElement logo = driver.findElement(By.id("logo"));
File part = logo.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(part, new File("element.png"));

8. How to pass test data to Selenium scripts?

From JSON: Used when your test data is stored in a .json file.

          JSONParser parser = new JSONParser();
          Object obj = parser.parse(new FileReader("data.json"));
          JSONObject json = (JSONObject) obj;
          String username = (String) json.get("username");
This code reads the JSON file, parses it, and fetches the value of username. Clean and easy if your data is in JSON format.

From Excel: Great when you want to maintain a table of data.

          FileInputStream file = new FileInputStream("testData.xlsx");
          XSSFWorkbook workbook = new XSSFWorkbook(file);
          XSSFSheet sheet = workbook.getSheet("Sheet1");
          String user = sheet.getRow(0).getCell(0).getStringCellValue();
          String pass = sheet.getRow(0).getCell(1).getStringCellValue();
          workbook.close();
This reads data from the first row and columns of an Excel sheet. Excel is very popular in real-world test management.

Using DataProvider:  Best for running the same test with different data sets.

          @DataProvider(name = "loginData")
          public Object[][] getData() {
                return new Object[][] {
                {"user1", "pass1"},
                {"user2", "pass2"}
                 };
                }
This supplies multiple sets of data to your test method. Very useful in data-driven testing.

9. How do you generate test reports in Selenium?

After running tests, we want to know what passed, what failed, and why. That’s where reports come in handy!
Here are some common tools:

  • TestNG Reports: Automatically generated HTML reports. Shows test results in a simple way. Works out-of-the-box with TestNG.
  • Extent Reports: A popular choice if you want visually rich reports with pie charts, screenshots, and step logs.
  • Allure Reports: Clean, modern-looking, and great for customization. It shows test steps, attachments, and logs in an organized format.
Why use them?  They help you or your team quickly understand how your automation tests performed. For failed tests, these tools can even capture screenshots and error logs for easy debugging.

10. What is Selenium Grid?

Imagine you're testing a website — but you want to check how it looks in Chrome, Firefox, and Edge, and maybe even on Windows and Mac. Doing that manually or one-by-one would take forever, right?

That’s where Selenium Grid helps.
It lets you run tests in parallel on multiple machines or browsers.
You set up a Hub (main controller) and multiple Nodes (worker machines).
The Hub sends tests to the right Node based on browser or OS needs.
This saves time and makes your automation truly scalable — perfect for cross-browser and cross-platform testing!

Wrapping Up

Interview prep doesn’t have to feel overwhelming. Tackle 10 questions each day, and by the end of the week, you’ll already feel more confident.
If today’s questions helped you, please share this post with your friends or teammates. It may be the quick revision they need before their interview! 📤
Until tomorrow, happy studying and keep pushing forward! 🚀



Previous Post Next Post