here are 50 intermediate-level Selenium interview questions with corresponding answers.
---
1. What is the Page Object Model (POM) in Selenium?
Answer: The Page Object Model is a design pattern in Selenium that creates an object repository for web UI elements. It helps in improving test maintenance and reducing code duplication by keeping the tests and page-specific elements separate.
2. How do you handle AJAX-based elements in Selenium?
Answer: To handle AJAX-based elements, you can use explicit waits to wait for specific conditions, such as element visibility or presence, using `WebDriverWait` and `ExpectedConditions`.
3. What is the use of `Actions` class in Selenium?
Answer: The `Actions` class is used to perform complex user interactions like drag and drop, click and hold, double-click, and right-click (context click) on web elements.
4. Write a code to perform a drag and drop operation in Selenium.
Answer:
```java
WebElement source = driver.findElement(By.id("source"));
WebElement target = driver.findElement(By.id("target"));
Actions action = new Actions(driver);
action.dragAndDrop(source, target).perform();
```
5. How do you handle a file upload in Selenium?
Answer: You can handle file upload by sending the file path to the input element of type `file` using `sendKeys()` method.
```java
WebElement uploadElement = driver.findElement(By.id("upload"));
uploadElement.sendKeys("/path/to/file");
```
6. Explain how to switch between multiple windows in Selenium.
Answer: You can switch between multiple windows using `driver.getWindowHandles()` to get all window handles and `driver.switchTo().window(windowHandle)` to switch to a specific window.
7. What is the difference between `driver.close()` and `driver.quit()` in Selenium?
Answer: `driver.close()` closes the current browser window, while `driver.quit()` closes all browser windows and ends the WebDriver session.
8. How do you handle pop-up windows in Selenium?
Answer: Pop-up windows can be handled using `driver.getWindowHandles()` to get all window handles and then switching to the pop-up window using `driver.switchTo().window(windowHandle)`.
9. Write a code to take a screenshot in Selenium.
Answer:
```java
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("/path/to/screenshot.png"));
```
10. What is Fluent Wait in Selenium?
Answer: Fluent Wait in Selenium defines the maximum amount of time to wait for a condition and the frequency to check the condition before throwing an exception. It also allows ignoring specific types of exceptions.
11. How do you handle JavaScript alerts and confirmations in Selenium?
Answer: You can handle JavaScript alerts and confirmations using the `Alert` interface.
```java
Alert alert = driver.switchTo().alert();
alert.accept(); // to click OK
alert.dismiss(); // to click Cancel
```
12. How do you verify if an element is visible on the web page?
Answer: You can verify if an element is visible using the `isDisplayed()` method.
```java
boolean isVisible = driver.findElement(By.id("element")).isDisplayed();
```
13. What is the use of `executeScript()` method in Selenium?
Answer: The `executeScript()` method is used to execute JavaScript code within the context of the browser. It can be used for scrolling, generating alerts, and more.
```java
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
```
14. How do you handle frames in Selenium?
Answer: You can switch to a frame using `driver.switchTo().frame()` by index, name, or WebElement, and switch back to the main content using `driver.switchTo().defaultContent()`.
15. Write a code to handle a dropdown in Selenium.
Answer:
```java
Select dropdown = new Select(driver.findElement(By.id("dropdown")));
dropdown.selectByVisibleText("Option 1");
dropdown.selectByValue("value1");
dropdown.selectByIndex(1);
```
16. How do you handle dynamic elements in Selenium?
Answer: Dynamic elements can be handled using robust locators like XPath or CSS selectors, and by using explicit waits to wait for elements to be present or visible.
17. Explain the use of `findElements()` method in Selenium.
Answer: The `findElements()` method returns a list of all WebElements that match the locator criteria. It returns an empty list if no elements are found.
18. Write a code to scroll to the bottom of the page in Selenium.
Answer:
```java
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
```
19. How do you handle SSL certificate errors in Selenium?
Answer: SSL certificate errors can be handled by configuring the browser to accept insecure certificates. For example, in Chrome:
```java
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(capabilities);
```
20. What is the use of `WebDriverWait` in Selenium?
Answer: `WebDriverWait` is used for explicit waits to wait for certain conditions to occur before proceeding further in the code. It works with `ExpectedConditions` to wait for elements to be visible, clickable, etc.
21. How do you perform right-click action in Selenium?
Answer: Right-click actions can be performed using the `Actions` class.
```java
Actions action = new Actions(driver);
action.contextClick(driver.findElement(By.id("element"))).perform();
```
22. How do you handle cookies in Selenium?
Answer: Cookies can be handled using methods like `driver.manage().getCookies()`, `driver.manage().addCookie()`, `driver.manage().deleteCookie()`, and `driver.manage().deleteAllCookies()`.
23. Explain how to use the `@FindBy` annotation in Selenium.
Answer: The `@FindBy` annotation is used in Page Object Model to locate web elements using different locators like ID, name, XPath, etc.
```java
@FindBy(id = "elementId")
WebElement element;
```
24. Write a code to verify the title of a web page in Selenium.
Answer:
```java
String expectedTitle = "Expected Title";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
```
25. How do you run Selenium tests in parallel?
Answer: Selenium tests can be run in parallel using TestNG by configuring the `testng.xml` file to include parallel execution settings.
```xml
```
26. Explain how to capture the text of a tooltip in Selenium.
Answer:
```java
WebElement element = driver.findElement(By.id("tooltipElement"));
String tooltipText = element.getAttribute("title");
```
27. Write a code to check if a checkbox is selected in Selenium.
Answer:
```java
boolean isSelected = driver.findElement(By.id("checkbox")).isSelected();
```
28. How do you perform double-click action in Selenium?
Answer: Double-click actions can be performed using the `Actions` class.
```java
Actions action = new Actions(driver);
action.doubleClick(driver.findElement(By.id("element"))).perform();
```
29. Explain how to perform keyboard actions in Selenium.
Answer: Keyboard actions can be performed using the `Actions` class or `sendKeys()` method.
```java
Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER).perform();
```
30. How do you handle `NoSuchElementException` in Selenium?
Answer: `NoSuchElementException` can be handled by using explicit waits to wait for elements to be present or by verifying the element's existence before interacting with it.
31. Write a code to handle a confirmation alert in Selenium.
Answer:
```java
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.accept(); // to click OK
```
32. Explain the use of `implicitlyWait()` in Selenium.
Answer: `implicitlyWait()` sets a default wait time for the WebDriver to wait for elements to appear. It applies globally to all elements in the script.
```java
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
```
33. How do you handle dropdowns without using the `Select` class?
Answer: Dropdowns can be handled by clicking on the dropdown and then selecting the desired option using XPath or CSS selectors.
```java
driver.findElement(By.id("dropdown")).click();
driver.findElement(By.xpath("//option[@value='value1']")).click();
```
34. Write a code to validate if a text is present on a web page.
Answer:
```java
boolean isTextPresent = driver.getPageSource().contains("Expected Text");
Assert.assertTrue(isTextPresent);
```
35. How do you handle StaleElementReferenceException in Selenium?
Answer: StaleElementReferenceException can be handled by re-locating the element or by using explicit waits to wait for the element to be stable before interacting with it.
36. What are the advantages of using Selenium Grid?
Answer: Selenium Grid allows for parallel test execution on multiple machines and browsers, reducing the overall test execution time and providing better test coverage.
37. Write a code to fetch the value of a hidden element in Selenium.
Answer:
```java
String value = ((JavascriptExecutor)driver).executeScript("return document.getElementById('hiddenElement').
value").toString();
```
38. How do you handle AJAX calls in Selenium?
Answer: AJAX calls can be handled using explicit waits to wait for the asynchronous request to complete and the elements to be updated or present in the DOM.
39. Explain how to perform a mouse hover action in Selenium.
Answer:
```java
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.id("element"))).perform();
```
40. How do you handle a scenario where an element is not clickable at point in Selenium?
Answer: You can handle this scenario by using JavaScript to click the element or by using explicit waits to wait for the element to be clickable.
```java
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
```
41. What is the use of `driver.manage().window().maximize()`?
Answer: The `driver.manage().window().maximize()` method maximizes the browser window to ensure all elements are visible and interactable.
42. How do you perform scrolling to a specific element in Selenium?
Answer:
```java
WebElement element = driver.findElement(By.id("element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
```
43. Write a code to handle multiple checkboxes in Selenium.
Answer:
```java
List
checkboxes = driver.findElements(By.name("checkboxes"));
for (WebElement checkbox : checkboxes) {
if (!checkbox.isSelected()) {
checkbox.click();
}
}
```
44. How do you handle browser navigation in Selenium?
Answer: Browser navigation can be handled using `driver.navigate().to()`, `driver.navigate().back()`, `driver.navigate().forward()`, and `driver.navigate().refresh()`.
45. What is the use of `DesiredCapabilities` in Selenium?
Answer: `DesiredCapabilities` is used to define browser-specific properties and configurations like setting browser preferences, enabling SSL certificates, and setting browser versions.
46. Explain how to capture network traffic in Selenium.
Answer: Network traffic can be captured using browser-specific tools or integrating Selenium with tools like BrowserMob Proxy or using Chrome DevTools Protocol (CDP).
47. Write a code to verify if an element is enabled in Selenium.
Answer:
```java
boolean isEnabled = driver.findElement(By.id("element")).isEnabled();
```
48. How do you handle date pickers in Selenium?
Answer: Date pickers can be handled by interacting with the input fields directly or selecting dates from the date picker widget using appropriate locators.
49. Explain the use of `@CacheLookup` annotation in Selenium.
Answer: The `@CacheLookup` annotation in Page Object Model is used to cache the WebElement once it is located, improving test performance by reducing the number of times an element is searched.
50. How do you run Selenium tests in headless mode?
Answer: Selenium tests can be run in headless mode by configuring the browser options. For example, in Chrome:
```java
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
```
---
These questions and answers will help intermediate-level candidates prepare for their Selenium interviews.