Selenium Ultimate Interview Preparation Guide (Advanced Level)
✅ 1. What is Selenium?
Selenium is an open-source suite of tools for automating web browsers. It supports multiple
languages like Java, Python, C#, Ruby, and supports major browsers.
Components:
● Selenium WebDriver
● Selenium Grid
● Selenium IDE
✅ 2. Selenium WebDriver Basics (with Examples):
Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement element = driver.findElement(By.id("username"));
element.sendKeys("testuser");
✅ 3. Locators and XPath Techniques:
● By ID, Name, ClassName, LinkText, PartialLinkText, TagName
● CSS Selectors Examples:
Java
// By class
By.cssSelector(".login-btn");
// By ID
By.cssSelector("#username");
// Descendant
By.cssSelector("div.container input[name='email']");
●
Advanced XPath Examples:
Java
//input[@type='text' and @name='username']
//*[contains(text(),'Welcome')]
div[contains(@class, 'menu')]//a[starts-with(text(),'Home')]
●
XPath Axes Examples:
Java
//following::input[1]
//preceding::label[1]
//ancestor::div
//descendant::span
//parent::div
//child::li
✅ 4. Handling Dropdowns:
Java
Select select = new
Select(driver.findElement(By.id("dropdown")));
select.selectByVisibleText("Option 1");
✅ 4.1. Auto Dropdown (Visible on click):
Java
driver.findElement(By.id("dropdownTrigger")).click();
driver.findElement(By.xpath("//li[text()='Option 3']")).click();
✅ 4.2. Auto Suggestive Dropdown:
Java
driver.findElement(By.id("searchBox")).sendKeys("test");
Thread.sleep(2000);
List<WebElement> options =
driver.findElements(By.xpath("//ul/li"));
for (WebElement option : options) {
if(option.getText().equals("Test Option")) {
option.click();
break;
}
}
✅ 5. Handling Alerts and Pop-ups:
Java
Alert alert = driver.switchTo().alert();
alert.accept();
✅ 6. Handling Windows, Frames, and iFrames:
Java
// Multiple Windows
String parent = driver.getWindowHandle();
for(String win : driver.getWindowHandles()) {
driver.switchTo().window(win);
}
// iFrame
WebElement frame =
driver.findElement(By.xpath("//iframe[@id='myFrame']"));
driver.switchTo().frame(frame);
driver.findElement(By.id("insideFrame")).click();
driver.switchTo().defaultContent();
✅ 7. Robot Class for File Upload:
Java
StringSelection file = new
StringSelection("C:\\path\\to\\file.txt");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(file
, null);
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);
✅ 8. JavaScriptExecutor:
Java
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", element);
✅ 9. Page Object Model (POM):
● Without PageFactory:
Java
public class LoginPage {
WebDriver driver;
By username = By.id("user");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String user) {
driver.findElement(username).sendKeys(user);
}
}
●
With PageFactory:
Java
public class LoginPage {
@FindBy(id = "user")
WebElement username;
public LoginPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
public void login(String user) {
username.sendKeys(user);
}
}
Advantages: Clean, readable, reusable code.
Disadvantages: Debugging with annotations can be tricky.
✅ 10. ThreadLocal WebDriver for Parallel Execution:
Java
public class DriverFactory {
private static ThreadLocal<WebDriver> driver = new
ThreadLocal<>();
public static WebDriver getDriver() {
return driver.get();
}
public static void setDriver(WebDriver drv) {
driver.set(drv);
}
}
✅ 11. Selenium Grid with Docker:
1. Install Docker
2. Start Hub and Nodes using Docker Compose:
Unset
version: "3"
services:
hub:
image: selenium/hub
ports:
- "4444:4444"
chrome:
image: selenium/node-chrome
depends_on:
- hub
environment:
- HUB_HOST=hub
3.
Run tests:
Java
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
WebDriver driver = new RemoteWebDriver(new
URL("http://localhost:4444/wd/hub"), capabilities);
✅ 12. WebDriverManager for Driver Management:
Java
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
✅ 13. Detect Broken Links:
Java
List<WebElement> links = driver.findElements(By.tagName("a"));
for(WebElement link : links){
String url = link.getAttribute("href");
if(url == null || url.isEmpty()) continue;
try {
HttpURLConnection conn = (HttpURLConnection)new
URL(url).openConnection();
conn.setRequestMethod("HEAD");
conn.connect();
int code = conn.getResponseCode();
if(code >= 400){
System.out.println("Broken: " + url);
} else {
System.out.println("Valid: " + url);
}
} catch (Exception e) {
System.out.println("Error with: " + url);
}
}
✅ 14. Excel Utility for Read/Write:
Java
FileInputStream fis = new FileInputStream("data.xlsx");
Workbook wb = WorkbookFactory.create(fis);
Sheet sheet = wb.getSheet("Sheet1");
String value = sheet.getRow(0).getCell(0).getStringCellValue();
// Write
sheet.getRow(0).createCell(1).setCellValue("Pass");
FileOutputStream fos = new FileOutputStream("data.xlsx");
wb.write(fos);
fos.close();
wb.close();
fis.close();
✅ 15. Actions Class:
Java
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
actions.doubleClick(element).perform();
actions.contextClick(element).perform();
actions.dragAndDrop(source, target).perform();
✅ 16. Chrome and Firefox Options:
Java
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
options.setAcceptInsecureCerts(true);
WebDriver driver = new ChromeDriver(options);
FirefoxOptions fOptions = new FirefoxOptions();
fOptions.setAcceptInsecureCerts(true);
✅ 17. ITestListener Example:
Java
public class Listener implements ITestListener {
public void onTestSuccess(ITestResult result) {
System.out.println("Test Passed: " + result.getName());
}
public void onTestFailure(ITestResult result) {
System.out.println("Test Failed: " + result.getName());
TakesScreenshot ts = (TakesScreenshot)
DriverFactory.getDriver();
File src = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(src, new File("screenshots/" +
result.getName() + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
✅ 18. Reporting with Extent Reports:
Java
ExtentReports extent = new ExtentReports();
ExtentSparkReporter spark = new
ExtentSparkReporter("report.html");
extent.attachReporter(spark);
ExtentTest test = extent.createTest("Login Test");
test.pass("Login successful");
test.fail("Login
failed").addScreenCaptureFromPath("screenshots/test.png");
extent.flush();
✅ 19. Soft Assert Example:
Java
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(actual, expected);
softAssert.assertTrue(condition);
softAssert.assertAll();
✅ 20. OOPs Concepts in Selenium:
● Encapsulation: Used in POM classes to encapsulate locators and actions
● Inheritance: BaseTest class extending utilities
● Abstraction: Interface for browser drivers
● Polymorphism: Method overriding with WebDriver interface
✅ 21. Common Selenium Challenges:
● Handling dynamic elements
● Dealing with slow-loading pages (use FluentWait)
● Uploading files in non-input elements
● Cross-browser compatibility
● Handling CAPTCHA (can’t be automated fully)
✅ 22. Designing a Framework (Key Modules):
● Base class
● Utility classes (Excel, Config, Screenshot, WaitUtils)
● Page classes (POM)
● Configuration (config.properties)
● Logging (Log4j or SLF4J)
● Reports (Extent)
● Driver management (ThreadLocal)
● TestNG or JUnit
● Data driven (Excel/CSV/JSON)
● Retry, Listeners, Parallel
✅ 23. Interview Questions:
1. Differences between Selenium 3 vs 4?
2. How to achieve parallel execution?
3. How ThreadLocal is used in Selenium?
4. What are Relative Locators?
5. Selenium Grid - how and why?
6. Robot Class vs AutoIT?
7. PageFactory pros/cons?
8. How to capture logs and screenshots on failure?
9. How to manage reusable locators?
10.Which wait to use and why?
11.How to upload files using sendKeys?
12.How to design a scalable automation framework?
13.Differences between Assert and Verify?
14.Implicit, Explicit, Fluent Wait?
15.Docker in Selenium use cases?
16.How to configure ITestListeners?
17.What’s new in Selenium 4?
18.What are CDP commands in Selenium 4?
19.How to work with shadow DOM?
Answer: Selenium does not support Shadow DOM directly. You need to use
JavaScriptExecutor:
Java
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement shadowHost =
driver.findElement(By.cssSelector("shadow-host-selector"));
WebElement shadowRoot = (WebElement) js.executeScript("return
arguments[0].shadowRoot", shadowHost);
WebElement elementInsideShadowDom =
shadowRoot.findElement(By.cssSelector("#shadow-element"));
elementInsideShadowDom.click();
20.
In Selenium 4+, you can use CDP (Chrome DevTools Protocol) commands or tools like
Playwright for better shadow DOM handling.
21.Explain OOPs in Selenium?
22.Explain iframe handling and window switching.
23.Difference between soft assert and hard assert?
24.Challenges faced and how you solved them?
💡
This is your ultimate and in-depth Selenium interview preparation doc . Everything from
✅
basic to advanced, parallelism to Docker, POM to Extent Reporting, and challenges to
OOPs — all in one go!