Selenium

Q1: What is Selenium?

Answer:
Selenium is an open-source automation testing framework used to automate web application testing. It enables testers to interact with web browsers and simulate user actions such as clicking buttons, entering text, selecting options, and navigating between pages. Selenium supports multiple browsers and programming languages.


Q2: What are the different components of Selenium?

Answer:
The Selenium suite consists of the following components:

  • Selenium WebDriver – Automates browser actions.
  • Selenium IDE – A record-and-playback tool for creating test scripts.
  • Selenium Grid – Executes tests on multiple browsers and machines simultaneously.
  • Selenium RC (Remote Control) – An older component that has been deprecated and replaced by WebDriver.

Q3: What is WebDriver in Selenium?

Answer:
WebDriver is the core component of Selenium that directly communicates with web browsers through browser-specific drivers (such as ChromeDriver or GeckoDriver). It provides APIs to automate browser actions without requiring a separate server.


Q4: Which programming languages are supported by Selenium WebDriver?

Answer:
Selenium WebDriver supports multiple programming languages, including:

  • Java
  • Python
  • C#
  • JavaScript
  • Ruby
  • Kotlin
  • PHP

This allows testers to write automation scripts using the language they are most comfortable with.


Q5: How can you handle AJAX calls in Selenium WebDriver?

Answer:
AJAX elements load asynchronously, so Selenium uses Explicit Wait to wait until the required element is available.

Example (Java):

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

Explicit Wait ensures the script proceeds only after the AJAX request is completed.


Q6: What is the difference between findElement() and findElements() in Selenium?

Answer:

findElement()findElements()
Returns the first matching elementReturns all matching elements as a list
Throws NoSuchElementException if no element is foundReturns an empty list if no elements are found
Used when only one element is expectedUsed when multiple matching elements are expected

Q7: How do you handle Frames in Selenium WebDriver?

Answer:
Selenium provides the switchTo().frame() method to switch into a frame.

Example:

 
driver.switchTo().frame("frameName");
driver.switchTo().frame(0);
driver.switchTo().frame(webElement);
 

To return to the main page:

 
driver.switchTo().defaultContent();
 

Q8: How do you handle Alerts in Selenium WebDriver?

Answer:
Selenium uses the Alert interface to interact with JavaScript alerts.

Common methods include:

  • accept() – Clicks OK.
  • dismiss() – Clicks Cancel.
  • getText() – Retrieves alert text.
  • sendKeys() – Enters text into a prompt alert.

Example:

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

Q9: What is the Page Object Model (POM)?

Answer:
Page Object Model (POM) is a design pattern that separates test scripts from page objects. Each web page is represented as a separate Java class containing web elements and reusable methods.

Advantages:

  • Better code organization
  • Easy maintenance
  • Reusable code
  • Improved readability

Q10: How do you handle dynamic web elements in Selenium?

Answer:
Dynamic elements can be handled using:

  • Relative XPath
  • CSS Selectors
  • Dynamic XPath
  • Explicit Wait
  • Contains() and Starts-with() XPath functions

Example:

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

Q11: What is TestNG? How is it useful in Selenium?

Answer:
TestNG is a Java testing framework used with Selenium to organize and execute test cases efficiently.

Features:

  • Test annotations
  • Parallel execution
  • Data Providers
  • Test grouping
  • Dependency management
  • HTML reports
  • Assertions

Q12: How do you handle browser cookies in Selenium WebDriver?

Answer:
Selenium provides methods to manage cookies.

Examples:

 
driver.manage().getCookies();
driver.manage().addCookie(cookie);
driver.manage().deleteCookie(cookie);
driver.manage().deleteAllCookies();
 

Cookies help in session management and login automation.


Q13: What is a Data-Driven Framework in Selenium?

Answer:
A Data-Driven Framework separates test data from test scripts. Test data is stored externally in Excel, CSV, JSON, XML, or databases, allowing the same test script to execute with multiple datasets.

Benefits:

  • Code reusability
  • Easier maintenance
  • Increased test coverage

Q14: How do you capture screenshots in Selenium?

Answer:
Selenium uses the TakesScreenshot interface.

Example:

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

Screenshots are useful for debugging failed test cases.


Q15: What is the difference between Assert and Verify?

Answer:

AssertVerify
Stops execution if validation failsContinues execution even if validation fails
Used in TestNG/JUnitCommonly implemented using Soft Assertions or custom verification
Suitable for critical validationsSuitable for non-critical validations

Q16: What is the difference between XPath and CSS Selectors?

Answer:

XPathCSS Selector
Supports forward and backward traversalSupports only forward traversal
Can locate elements using textCannot locate by text
More flexibleFaster in most cases
More complex syntaxSimpler syntax

Q17: How do you handle multiple browser windows in Selenium?

Answer:
Use getWindowHandles() to retrieve all window IDs and switchTo().window() to move between windows.

Example:

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

for(String win : windows){
    driver.switchTo().window(win);
}
 

Q18: What are the advantages of Selenium?

Answer:

Some major advantages include:

  • Open-source and free
  • Supports multiple browsers
  • Supports multiple programming languages
  • Cross-platform compatibility
  • Integrates with TestNG, JUnit, Maven, Jenkins, Git, and CI/CD tools
  • Large community support
  • Supports parallel execution using Selenium Grid

Q19: How do you handle dropdowns in Selenium WebDriver?

Answer:
Selenium provides the Select class to interact with HTML dropdowns.

Common methods:

  • selectByVisibleText()
  • selectByValue()
  • selectByIndex()

Example:

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