Selenium Tricky Interview Questions with Answers

How to handle CAPTCHA in Selenium?

To handle CAPTCHA in Selenium, we generally need to use third-party services or manual intervention as Selenium cannot solve CAPTCHA directly. We can use services like 2Captcha, Anti-CAPTCHA, etc., to solve CAPTCHA. To handle captcha manually, use Thread.sleep() to wait until the CAPTCHA text is entered by a user before proceeding with the script.

How to handle hidden elements in Selenium?

Hidden elements are not visible on the web page but can be accessed using Selenium by executing JavaScript since the elements are present in the DOM. We can use the executeScript() method of the JavascriptExecutor. To handle hidden elements in Selenium, we can use the below code snippet:

WebElement element = driver.findElement(By.id("hiddenElement"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);

How to handle infinite scrolling pages in Selenium?

Infinite scrolling pages load content dynamically as the user scrolls down the page like social media feeds, search results, etc. To handle such pages in Selenium, we have to use the window.scrollTo() method inside an infinite loop to keep on scrolling to the bottom of the page and load all the content. Below is the code example:

JavascriptExecutor js = (JavascriptExecutor) driver;
long lastHeight = (long) js.executeScript("return document.body.scrollHeight");
while (true) {
  js.executeScript("window.scrollTo(0, document.body.scrollHeight);");
  Thread.sleep(2000);
  long newHeight = (long) js.executeScript("return document.body.scrollHeight");
  if (newHeight == lastHeight) {
    break;
  }
  lastHeight = newHeight;
}
In above code snippet, we are scrolling to the bottom of the page and checking if the page height has changed. If the page height remains the same, it means we have reached the end of the page and all content is loaded.

How to handle touch actions in Selenium?

In some scenarios, we need to handle touch actions like swipe, scroll, etc., in Selenium. We can use the TouchActions class to handle touch actions in Selenium. Below is the code example:

WebElement element = driver.findElement(By.id("element"));
TouchActions actions = new TouchActions(driver);
actions.scroll(element, 10, 100);
actions.perform();

How to disable browser notifications in Selenium?

Sometimes browser notifications can interfere with the automation script. To disable browser notifications in Selenium, we can use ChromeOptions with --disable-notifications argument as below

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
WebDriver driver = new ChromeDriver(options);

How to handle broken links in Selenium?

This is a most common tricky interview question. In this scenario, we need to find all the links on a web page and check if they are broken or not. To handle broken links in Selenium, we have to loop through all the links on the page and check the response code of each link. If the response code is greater than or equal to 400, it means the link is broken. Below is the code example:

List links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
  HttpURLConnection connection = (HttpURLConnection) new URL(link.getAttribute("href")).openConnection();
  connection.connect();
  int responseCode = connection.getResponseCode();
  connection.disconnect();
  if (responseCode >= 400) {
    System.out.println("Broken Link: " + link.getAttribute("href"));
  }
}

How to handle browser tabs in Selenium?

This is similar to handling multiple windows in Selenium. We simply switch between the tabs using driver.switchTo().window(handle) method. To handle browser tabs in Selenium, we have to loop through all the window handles and switch to the desired tab. Below is the code example:

Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
  driver.switchTo().window(handle);
  System.out.println(driver.getTitle());
}

How to handle right click in Selenium?

This is simple yet tricky interview question. To handle right click in Selenium, we can use the Actions class to perform context click on an element. Below is the code example:

WebElement element = driver.findElement(By.id("element"));
Actions actions = new Actions(driver);
actions.contextClick(element).perform();

How to handle clipboard operations in Selenium?

This is asked to check if the candidate is aware of the clipboard operations in Selenium. To perform clipboard operations in Selenium, we can use the Actions class to interact with the clipboard. Below is the code example:

WebElement element = driver.findElement(By.id("element"));
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();
actions.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).perform();

What is difference between click() and clickViaJavaScript() in Selenium?

The click() method is used to perform a normal click operation on an element, whereas the clickViaJavaScript() method is used to perform a click operation using JavaScript. The clickViaJavaScript() method is useful when the normal click operation does not work due to some reasons like hidden elements, disabled elements, etc. Here is the code example:

WebElement element = driver.findElement(By.id("element"));
element.click(); // Normal click
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element); // Click using JavaScript

How to handle browser window close confirmation in Selenium?

In some scenarios, when we try to close the browser window, a confirmation dialog is displayed to the user to confirm whether to close the window or not because of unsaved changes. To handle browser window close confirmation in Selenium, we can use the below code snippet:

driver.switchTo().alert().accept(); // To accept the confirmation dialog
driver.switchTo().alert().dismiss(); // To dismiss the confirmation dialog

How to handle stale element with custom retry in Selenium?

We may get StaleElementReferenceException when the element is no longer valid for Selenium to interact with. To handle stale elements with retry mechanism, we can use a custom retry logic to find the element again and perform the action. For that we need to loop for a few attempts and try to find the element and in case of StaleElementReferenceException exception, we can retry to find the element again. Below is the code example:

int attempts = 0;
while (attempts < 2) {
  try {
    WebElement element = driver.findElement(By.id("element"));
    element.click();
    break;
  } catch (StaleElementReferenceException e) {
    attempts++;
  }
}

How to handle browser timeouts in Selenium?

Sometimes we need to handle browser timeouts in Selenium to avoid the script getting stuck. For example - If some page is taking too long to load, we can set a timeout for the page load. To handle browser timeouts in Selenium, we can use the below code snippet:


driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

How to handle browser incognito mode in Selenium?

In some case we need might need to use incognito mode in Selenium to avoid browser cache, cookies, etc. To handle browser incognito mode in Selenium, we can use ChromeOptions with --incognito argument as below:

ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
WebDriver driver = new ChromeDriver(options);

How to handle browser headless mode in Selenium?

Headless mode is used to run the browser without opening the GUI. This is useful when we want to run the automation script in the background without opening the browser window. To handle browser headless mode in Selenium, we can use ChromeOptions with --headless argument as below:

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);

How to handle custom HTML5 validation messages in Selenium?

HTML 5 validation messages are displayed when the form input does not meet the validation criteria like required, min, max, etc. near the input field. To handle HTML5 validation messages, we need to use JavaScriptExecutor to get the validation message. Below is the code example:

WebElement element = driver.findElement(By.id("element"));
JavascriptExecutor js = (JavascriptExecutor) driver;
String validationMessage = (String) js.executeScript("return arguments[0].validationMessage;", element);
System.out.println("Validation Message: " + validationMessage);

How to handle browser permissions in Selenium?

Some pages may ask for browser permissions like notifications, location, camera, etc. to access the features. To handle browser permissions in Selenium, we can use ChromeOptions with prefs argument as below:


ChromeOptions options = new ChromeOptions();
HashMap<String, Object> prefs = new HashMap<>();
prefs.put("profile.default_content_setting_values.notifications", 2);
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

How to handle WebSocket connections in Selenium?

In case a web page uses WebSocket connections, we can monitor WebSocket connections in Selenium using Chrome DevTools. To monitor WebSocket connections:


ChromeDevToolsService devToolsService = DevToolsService.getDevToolsService(driver);
devToolsService.send(WebSocket.enable(Optional.empty()));
devToolsService.addListener(WebSocketFrameReceived.class, webSocketFrameReceived -> {
  System.out.println("WebSocket Frame Received: " + webSocketFrameReceived);
});

How to handle dynamic mouse hover tooltips in Selenium?

In some scenarios, we need to handle dynamic mouse hover tooltips in Selenium. To handle dynamic mouse hover tooltips, we can use Actions class to move to the element and then wait for the tooltip to appear. Below is the code example:

WebElement element = driver.findElement(By.id("element"));
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("tooltip")));
If you have liked our content, please share it with your friends and colleagues.
We are still adding more tricky interview questions. Please check back later for more updates. You can also contribute by sending your tricky interview questions to Contact US page. We will include them in this list on priority.
Next we will cover Selenium Java Coding Interview Questions.