Wednesday, July 9, 2025

Selenium WebDriver Interview Questions with Java Code – Real-Time Examples

Selenium WebDriver Interview Questions with Java Code – Real-Time Examples

Are you preparing for a Selenium WebDriver interview? Below are the most frequently asked Selenium interview questions with Java code examples. These cover real-time scenarios and will help both freshers and experienced testers gain confidence.


1. What is Selenium WebDriver?

Selenium WebDriver is a web automation tool used to automate browser actions like clicking, typing, navigating, etc. It provides a programming interface to create and run test cases.

Java Example:

WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.findElement(By.id("login")).click();
---

2. How do you launch a browser in Selenium?

Using the WebDriver interface and browser-specific driver classes like ChromeDriver or FirefoxDriver.

Java Example:

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://google.com");
---

3. How to locate elements in Selenium?

Using locators: id, name, className, cssSelector, xpath, linkText, tagName

Java Example:

driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.name("password")).sendKeys("admin123");
driver.findElement(By.xpath("//button[@type='submit']")).click();
---

4. What is the difference between findElement() and findElements()?

findElement()findElements()
Returns single WebElementReturns a list of WebElements
Throws NoSuchElementException if not foundReturns empty list if not found
---

5. How do you handle dropdowns in Selenium?

Using the Select class.

Java Example:

WebElement dropdown = driver.findElement(By.id("country"));
Select select = new Select(dropdown);
select.selectByVisibleText("India");
---

6. How to handle alerts in Selenium?

Java Example:

Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept(); // or alert.dismiss();
---

7. How do you handle multiple windows in Selenium?

String parent = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
for (String window : allWindows) {
    if (!window.equals(parent)) {
        driver.switchTo().window(window);
        driver.close();
    }
}
driver.switchTo().window(parent);
---

8. How to perform mouse hover in Selenium?

Actions action = new Actions(driver);
WebElement menu = driver.findElement(By.id("menu"));
action.moveToElement(menu).perform();
---

9. How do you handle dynamic elements?

Use dynamic XPath or wait strategies like explicit wait.

Example using XPath:

//input[contains(@id, 'user')]

Example using Explicit Wait:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
---

10. What are Waits in Selenium?

  • Implicit Wait – waits globally
  • Explicit Wait – waits for specific conditions
  • Fluent Wait – waits with polling interval

Example – Implicit Wait:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
---

11. How do you take a screenshot in Selenium?

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

12. How do you handle frames?

driver.switchTo().frame("frameName");
// Perform actions inside frame
driver.switchTo().defaultContent();
---

13. How do you perform scrolling in Selenium?

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)");
---

14. How do you validate a page title or URL?

Assert.assertEquals(driver.getTitle(), "Expected Title");
Assert.assertTrue(driver.getCurrentUrl().contains("dashboard"));
---

15. What is Page Object Model (POM)?

POM is a design pattern where each web page is represented as a class. Elements are defined as variables, and actions are methods.

Example:

public class LoginPage {
  WebDriver driver;
  
  @FindBy(id="username") WebElement username;
  @FindBy(id="password") WebElement password;
  @FindBy(id="login") WebElement loginBtn;

  public LoginPage(WebDriver driver) {
    PageFactory.initElements(driver, this);
  }

  public void login(String user, String pass) {
    username.sendKeys(user);
    password.sendKeys(pass);
    loginBtn.click();
  }
}
---

16. How do you run tests in multiple browsers?

Use WebDriverManager and pass the browser as a parameter.

WebDriver driver;
if(browser.equals("chrome")){
   WebDriverManager.chromedriver().setup();
   driver = new ChromeDriver();
} else if(browser.equals("firefox")){
   WebDriverManager.firefoxdriver().setup();
   driver = new FirefoxDriver();
}
---

17. What is the difference between get() and navigate().to()?

  • get() – loads a new page
  • navigate().to() – can be used to go forward/backward in history
---

18. What is the difference between driver.close() and driver.quit()?

  • close() – closes current browser window
  • quit() – closes all windows and ends session
---

19. How to handle file upload?

driver.findElement(By.id("upload")).sendKeys("C:\\path\\file.txt");
---

20. How to handle file download in Selenium?

File download requires browser settings using ChromeOptions or FirefoxProfile to set download paths.

---

👋 Hi, I'm Suriya — QA Engineer with 4+ years of experience in manual, API & automation testing.

📬 Contact Me | LinkedIn | GitHub

📌 Follow for: Real-Time Test Cases, Bug Reports, Selenium Frameworks.

No comments:

Post a Comment