Advertisement

Responsive Ads Here

Saturday, August 30, 2025

Top 1 to 25 Selenium Automation Interview Questions and Answers (Basic to Advanced, with Real-Time Examples) – Part 1

      


1. What is Selenium?
Selenium is an open-source automation testing tool used for testing web applications. It supports multiple browsers like Chrome, Firefox, and Edge, and allows testers to write scripts in various programming languages such as Java, Python, and C#. Selenium only supports web applications, not desktop or mobile apps.

2. What are the components of Selenium?

Selenium has four main components:

  • Selenium IDE – Record and playback tool.
  • Selenium RC – (deprecated) allowed execution of test scripts.
  • Selenium WebDriver – Modern automation tool that directly interacts with browsers.
  • Selenium Grid – Used to run tests in parallel across multiple browsers and machines.

3. What are the advantages of using Selenium?

  • Open-source and free to use.
  • Supports multiple browsers and operating systems.
  • Supports different programming languages.
  • Can be integrated with tools like TestNG, JUnit, Maven, Jenkins, etc. for CI/CD.
  • Large community support.

4. What are the limitations of Selenium?

  • It only supports web applications, not desktop or mobile apps.
  • No built-in reporting feature (needs integration with other tools).
  • Does not handle captcha, barcode, OTP, or image-based testing directly.
  • Requires good programming knowledge.

5. What is Selenium WebDriver?

Selenium WebDriver is a tool used to automate web application testing. It directly communicates with the browser without requiring a server. It provides APIs to interact with web elements like buttons, input fields, and checkboxes.


6. What are locators in Selenium?

Locators are ways to identify elements on a web page so that Selenium can interact with them.

 Common locators are:

    •  id
    •  name
    •  className
    •  tagName
    •  linkText
    •  partialLinkText
    •  cssSelector
    •  xpath

7. How does Selenium WebDriver work internally? Can you explain the architecture?

Answer:

Selenium WebDriver follows a client–server architecture:

    • Test Script (Client Library) – We write our test script using Java, Python, C#, etc.
    • JSON Wire Protocol (or W3C protocol) – The script communicates with the browser driver via HTTP requests.
    • Browser Driver – Each browser has its own driver (ChromeDriver, GeckoDriver, etc.) which translates JSON commands into browser-specific native calls.
    • Browser – Finally, the browser executes the command and sends the response back to WebDriver.

      Example:

If I write:

driver.findElement(By.id("username")).sendKeys("TestUser");

    • Selenium converts this into a JSON command.
    • The command goes to ChromeDriver.
    • ChromeDriver sends the instruction to Chrome browser.
    • Browser types "TestUser" in the username field and sends success/failure back.

Smart Add-on (Real-time use):

In real projects, I explain this because sometimes tests fail due to mismatch between WebDriver and browser version – since communication happens via the driver.


8. Difference between findElement() and findElements()?

Answer:

  • findElement() → Returns the first matching element. If not found, it throws NoSuchElementException.
  • findElements() → Returns a list of all matching elements. If none are found, it returns an empty list (no exception).

Example:

driver.findElement(By.tagName("a"));     // Returns first <a> tag

driver.findElements(By.tagName("a"));    // Returns list of all <a> tags

Real-time Scenario:
In my project, when I had to validate menu items on a navigation bar, I used findElements() because I needed to count how many menu links were present. But while filling a form, I used findElement() because I only needed a single input field
.


9. How do you handle dynamic web elements (like IDs that keep changing)?

Answer:
Dynamic elements have changing attributes (like
id=user_1234, id=user_5678). To handle them:

  1. Use relative XPath with contains/starts-with:

driver.findElement(By.xpath("//input[contains(@id,'user_')]"));

  1. Use other stable attributes like name, class, aria-label, placeholder.
  2. Use parent-child relationships in XPath.
  3. Use CSS selectors if possible.

Real-time Example:
In one of my projects, the search bar’s id kept changing on every refresh. I solved it by locating using:

driver.findElement(By.xpath("//input[@placeholder='Search']"));

instead of using id.


10. What are different types of waits in Selenium? Can you explain implicit, explicit, and fluent wait with examples?

Answer:
Selenium provides three main waits to handle synchronization:

  1. Implicit Wait – Applied globally. Tells WebDriver to wait for a certain time before throwing NoSuchElementException.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  1. Explicit Wait – Applied to a specific element with certain condition.

WebDriverWait wait = new WebDriverWait(driver, 20);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("loginBtn")));

  1. Fluent Wait – Similar to Explicit Wait but lets you define polling frequency and ignore exceptions.

Wait<WebDriver> wait = new FluentWait<>(driver)

   .withTimeout(Duration.ofSeconds(30))

   .pollingEvery(Duration.ofSeconds(5))

   .ignoring(NoSuchElementException.class);

 

wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn")));

Real-time Example:
When automating a payment gateway flow, the confirmation message took time to appear. Initially, my script failed due to NoSuchElementException. I solved it by using Explicit Wait until the success message was visible
.



11. How to handle AJAX elements in Selenium?
Answer:

AJAX loads content asynchronously (without refreshing). Normal locators may fail if elements load late.
✅ Solution: Use Explicit or Fluent Wait.

Example:

WebDriverWait wait = new WebDriverWait(driver, 15);

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ajaxElement")));

Real-time Use:
In my e-commerce project, product results after applying filters were loaded via AJAX. I had to use Explicit Wait to ensure all results were displayed before validating.


12. Difference between driver.close() and driver.quit()?
Answer:

  • driver.close() → Closes current browser window only.
  • driver.quit() → Closes all browser windows opened by WebDriver and ends the session.

Example:
In a scenario where multiple popups open, I used driver.close() for a single popup but at the end of suite execution, I used driver.quit() to clean up everything
.


13. Can Selenium handle Windows-based popups? If not, alternatives?
Answer:
No, Selenium handles only web-based popups (alerts, confirms, prompts).
For Windows popups like file upload dialogs, we use:

  • Robot Class (Java)
  • AutoIT (Windows tool)
  • Sikuli (Image-based automation)

Example:
In one project, I handled a file upload popup using Robot class:

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);


14. Difference between getWindowHandles() and getWindowHandle()?
Answer:

  • getWindowHandle() → Returns the current window ID.
  • getWindowHandles() → Returns a set of all open window IDs.

Example:

String parent = driver.getWindowHandle();

Set<String> allWindows = driver.getWindowHandles();

I used this in real-time when automating login flows with third-party authentication popups (Google, Facebook).


15. How to handle multiple tabs/windows in Selenium?
Answer:
Steps:

  1. Store current window handle.
  2. Get all handles.
  3. Switch using driver.switchTo().window(handle).

Example:

String parent = driver.getWindowHandle();

for(String win : driver.getWindowHandles()) {

    if(!win.equals(parent)) {

        driver.switchTo().window(win);

    }

}

Real-time Use:
In telecom CRM testing, after clicking "Bill Download," a new tab opened. I switched to it, validated content, then switched back.


16. How to switch between frames (iFrame, nested frames) in Selenium?
Answer:
We can switch in 3 ways:

  1. By indexdriver.switchTo().frame(0);
  2. By name or IDdriver.switchTo().frame("frameName");
  3. By WebElement

WebElement frame = driver.findElement(By.xpath("//iframe[@id='frame1']"));

driver.switchTo().frame(frame);

To go back → driver.switchTo().defaultContent();

Real-time Example:
In Salesforce UI, many objects open in iFrames (like chatter feed). I had to switch into the frame to interact with the elements.


---------------------------------------------------------------------------------------------------------------------
17. How do you handle file uploads and downloads in Selenium?
Answer:

  • File Uploads → If the element is <input type="file">, we can directly use sendKeys().

driver.findElement(By.id("uploadBtn")).sendKeys("C:\\Users\\me\\file.txt");

  • If it opens a native dialog → Use Robot Class / AutoIT / Sikuli.
  • File Downloads → Configure browser preferences (like ChromeOptions or Firefox profile) to auto-download files to a specific location.

Real-time Example:
In my project, I automated invoice download by setting ChromeOptions with custom download directory so the PDF was saved automatically.


18. How to take a screenshot in Selenium?
Answer:

Using
TakesScreenshot interface:

File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(src, new File("screenshot.png"));

Real-time Use:
I integrated this with TestNG Listeners to automatically capture screenshots when a test fails. It helped in defect reporting.


19. How do you handle JavaScript alerts, confirmations, and prompts?
Answer:
Use
Alert interface.

Alert alert = driver.switchTo().alert();

alert.accept();       // OK

alert.dismiss();      // Cancel

alert.getText();      // Read text

alert.sendKeys("text"); // For prompt

Real-time Example:
While testing Salesforce, deleting a record triggered a confirmation alert. I used alert.accept() to confirm deletion.


20. Can you automate Captcha or OTP scenarios? If not, how do you handle them?
Answer:

Direct automation is not possible for Captcha/OTP because they are security features.
✅ Workarounds:

  • Captcha → Ask developers to provide a test bypass in QA environment.
  • OTP → Fetch from DB, API, or email/SMS service via integration.

Real-time Example:
In telecom onboarding, OTP was sent via SMS. I integrated Selenium with an API call to the message service to fetch OTP and auto-fill in the test.


21. What is Page Object Model (POM)? How do you implement it?
Answer:
POM is a design pattern where each web page is represented as a class and elements as variables, actions as methods.

Example:

public class LoginPage {

    WebDriver driver;

    By username = By.id("user");

    By password = By.id("pass");

    By loginBtn = By.id("login");

 

    public void login(String u, String p){

        driver.findElement(username).sendKeys(u);

        driver.findElement(password).sendKeys(p);

        driver.findElement(loginBtn).click();

    }

}

Real-time Use:
I implemented POM in my framework to reduce code duplication and improve maintainability when locators changed.


22. What is Page Factory in Selenium, and how is it different from POM?
Answer:

  • Page Factory is an extension of POM.
  • It uses @FindBy annotations with initElements() method.
  • It supports lazy initialization (elements loaded only when used).

Example:

public class LoginPage {

    @FindBy(id="user") WebElement username;

    @FindBy(id="pass") WebElement password;

    @FindBy(id="login") WebElement loginBtn;

 

    public LoginPage(WebDriver driver){

        PageFactory.initElements(driver, this);

    }

}

Difference:

  • POM → Manual element initialization.
  • Page Factory → Uses annotations + reduces boilerplate code.

23. How do you handle stale element reference exceptions?
Answer:
This happens when the element is no longer attached to DOM (like after refresh).
✅ Solutions:

  • Re-locate the element before using it.
  • Use Explicit Wait.
  • Wrap with retry logic.

Example:

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));

btn.click();

Real-time Use:
In Salesforce, after saving a record, page refreshed and old element became stale. I solved it by re-fetching the locator.


24. Explain the concept of synchronization in Selenium.
Answer:
Synchronization means making WebDriver wait until the webpage is ready before performing actions.

  • Without it → Tests fail due to timing issues.
  • Achieved using implicit, explicit, and fluent waits.

Real-time Example:
On my project, dynamic graphs took 5–10 seconds to load. Adding synchronization ensured my validations worked consistently.


25. How to execute JavaScript using Selenium WebDriver? Give examples.
Answer:
Use
JavascriptExecutor.

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("arguments[0].click();", element);  // Click

js.executeScript("window.scrollBy(0,500);");         // Scroll

js.executeScript("return document.title;");          // Get title

Real-time Use:
When normal
.click() failed due to overlay, I used JS click to bypass it.


Stay tuned for more questions.

                                                                    


No comments:

Post a Comment