
Top 50 Selenium Interview Questions and Answers: Basic, Intermediate, Advanced

Basic Selenium Questions
- What is Selenium?
Selenium is an open-source, automated testing framework for web applications across different browsers and platforms. It supports multiple programming languages such as Java, Python, C#, etc.
2. What are the different components of Selenium?
- Selenium IDE
- Selenium WebDriver
- Selenium Grid
3. What are the advantages of using Selenium?
- Open-source and free
- Supports multiple browsers and platforms
- Language support for test scripts
- Integration with CI/CD tools and frameworks
4. What are the limitations of Selenium?
- Only supports web application testing
- No built-in reporting functionality
- Requires external tools for test management
5. What are the supported programming languages in Selenium?
- Java
- Python
- C#
- Ruby
- JavaScript
- Kotlin
6. What is Selenium WebDriver?
Selenium WebDriver is a browser automation framework that accepts commands and sends them to a browser. It supports cross-browser testing.
7. What is the difference between Selenium WebDriver and Selenium IDE?
- Selenium IDE is a record-and-playback tool.
- Selenium WebDriver is used for more advanced browser automation with programming logic.
8. How do you locate an element in Selenium?
By using locators such as ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS Selector, and XPath.
9. What is XPath?
XPath is a syntax used to navigate through elements and attributes in an XML document. In Selenium, it helps locate elements on a webpage.
10. What are the types of XPath?
- Absolute XPath: Direct path starting from the root node (/).
- Relative XPath: Searches for the target element from anywhere in the document (//).
Intermediate Selenium Questions
11. What is the difference between findElement() and findElements()?
- findElement(): Returns a single WebElement.
- findElements(): Returns a list of WebElements.
12. How do you handle dynamic elements in Selenium?
Use dynamic XPath with attributes like contains, starts-with, or ends-with.
13. What is the difference between get() and navigate().to()?
- get(): Loads a webpage and waits until it is completely loaded.
- navigate().to(): Does not wait for the page to load.
14. What are implicit and explicit waits in Selenium?
- Implicit Wait: Sets a default wait time for all elements.
- Explicit Wait: Waits for a specific condition to be met for a specific element.
15. What is Fluent Wait?
Fluent Wait polls for a condition at regular intervals and allows customization of the wait conditions.
16. What is the use of switchTo() in Selenium?
It is used to switch the WebDriver’s context to frames, alerts, or windows.
17. How do you handle alerts in Selenium?
- driver.switchTo().alert().accept(); to accept an alert.
- driver.switchTo().alert().dismiss(); to dismiss an alert.
18. How do you handle multiple windows in Selenium?
Use getWindowHandles() to switch between windows using driver.switchTo().window(windowHandle).
19. How can you perform mouse hover actions in Selenium?
Use the Actions class and moveToElement() method:
java
Copy code
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();
20. What is the use of getWindowHandles() and getWindowHandle()?
getWindowHandle(): Retrieves the current window’s handle.
getWindowHandles(): Retrieves handles for all open windows.
Advanced Selenium Questions
21. How do you handle dropdowns in Selenium?
Use the Select class to interact with dropdown elements.
java
Copy code
Select select = new Select(driver.findElement(By.id("dropdown")));
select.selectByVisibleText("Option");
22. How do you upload a file in Selenium?
Use the sendKeys() method to provide the file path to the input element.
23. How do you take a screenshot in Selenium?
Use TakesScreenshot interface:
java
Copy code
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("path/to/save.png"));
24. What is Page Object Model (POM)?
POM is a design pattern that creates an object repository for web elements to enhance test maintenance and reduce code duplication.
25. What are the advantages of using POM?
- Improves test readability and maintenance
- Reusability of code
- Reduces duplication
26. How do you handle iframes in Selenium?
Switch to an iframe using:
java
Copy code
driver.switchTo().frame(index/name/WebElement);
27. How do you handle JavaScript pop-ups in Selenium?
Use executeScript() method from the JavascriptExecutor interface.
28. What is the executeScript() method?
It is used to execute JavaScript directly in the browser. Example:
java
Copy code
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
29. How do you verify tooltips in Selenium?
Capture the tooltip using the getAttribute("title") method.
30. What is a Hybrid Framework in Selenium?
A Hybrid Framework is a combination of two or more frameworks, such as Data-Driven and Keyword-Driven.
Real-World Selenium Questions
31. How do you handle Stale Element Reference Exception?
Re-locate the element before interacting with it.
32. How do you scroll a webpage using Selenium?
Use the JavaScriptExecutor:
java
Copy code
js.executeScript("window.scrollBy(0,1000)");
33. What is the difference between quit() and close()?
- quit(): Closes all browser windows opened by WebDriver.
- close(): Closes the current browser window.
34. How do you perform drag and drop in Selenium?
Use the Actions class:
java
Copy code
actions.dragAndDrop(source, target).perform();
35. How can you maximize the browser window in Selenium?
java Copy code driver.manage().window().maximize();
36. What is the difference between CSS Selector and XPath?
- CSS Selector is faster but less flexible than XPath.
- XPath allows for navigation through XML structure and supports dynamic element identification.
37. How do you handle SSL Certificate errors in Selenium?
Use DesiredCapabilities or browser options to ignore SSL errors.
38. How do you capture console logs using Selenium?
Use the LogEntries API provided by the WebDriver.
39. What is TestNG, and how does it relate to Selenium?
TestNG is a testing framework that integrates with Selenium for test case organization, parallel execution, and reporting.
40. What is an Assertion in Selenium?
Assertions are used to validate expected outcomes in test cases.
Practical Selenium Questions
41. How do you handle CAPTCHA in Selenium?
CAPTCHA cannot be automated. Use workarounds like disabling CAPTCHA in test environments or manual intervention.
42. How do you perform database testing in Selenium?
Use JDBC to connect with databases and execute queries.
43. How do you verify broken links using Selenium?
Use HTTP responses via APIs or libraries like Apache HttpClient.
44. How do you run tests in parallel using Selenium?
Use TestNG or the Selenium Grid for parallel execution.
45. What is Selenium Grid?
A tool that enables running tests on multiple browsers and machines simultaneously.
46. How do you set browser preferences in Selenium?
Use ChromeOptions or FirefoxOptions.
47. How do you verify page titles in Selenium?
java
Copy code
String title = driver.getTitle();
Assert.assertEquals(title, "Expected Title");
48. What are DesiredCapabilities in Selenium?
DesiredCapabilities configure WebDriver instances like browser name, version, and platform.
49. How do you interact with hidden elements in Selenium?
Use JavaScriptExecutor to click or send keys.
50. How do you debug Selenium scripts?
Use breakpoints, debugging tools in IDEs, or add logging statements to track script execution.