Selenium Basic Interview Questions

In this article, we will cover some of the most common Selenium Basic interview questions. These questions are designed to test your knowledge of Selenium basics, including WebDriver, locators, waits, and handling alerts. Whether you are a beginner or looking to refresh your Selenium skills, these questions will help you prepare for your next interview.

What is Selenium?

OR

Why to use Selenium?

Selenium is an open-source tool for automating web browsers. It supports multiple programming languages like Java, C#, Python, and allows you to write test scripts that can run across different browsers.

What are the different components of Selenium?

Selenium consists of four main components: Selenium IDE (a record and playback tool), Selenium RC (Remote Control), WebDriver (a programming interface for browser automation), and Selenium Grid (for running tests on multiple machines).

How Selenium and Selenium WebDriver different?

Selenium is a collection of tools for automating web browsers, which includes Selenium IDE, Selenium WebDriver, and Selenium Grid. Selenium WebDriver is a part of the Selenium suite that provides classes and methods to create and execute test scripts. It directly communicates with the browser without any intermediary server, making it faster and more efficient.

What are the browsers supported by Selenium?

Selenium supports multiple browsers for automation. These include:

  • Google Chrome: Use the ChromeDriver class.
  • Mozilla Firefox: Use the GeckoDriver class.
  • Microsoft Edge: Use the EdgeDriver class.
  • Safari: Use the SafariDriver class on macOS.
  • Internet Explorer: Use the IEDriverServer class.
  • Opera: Use the OperaDriver class.

What is a locator in Selenium?

A locator in Selenium is a way to identify HTML elements on a web page. Common locators include ID, name, class name, tag name, link text, partial link text, CSS selector, and XPath. Example:

WebElement element = driver.findElement(By.id("exampleId"));

What are the different types of locators in Selenium?

Selenium provides several types of locators to identify HTML elements on a web page. These include:

  • ID: Locates elements by their unique ID attribute.
  • Name: Locates elements by their name attribute.
  • Class Name: Locates elements by their class attribute.
  • Tag Name: Locates elements by their HTML tag name.
  • Link Text: Locates anchor elements by their visible text.
  • Partial Link Text: Locates anchor elements by a partial match of their visible text.
  • CSS Selector: Locates elements using CSS selectors.
  • XPath: Locates elements using XPath expressions.

What is XPath?

XPath is a language used for locating nodes in XML documents. In Selenium, XPath is used to locate elements on a web page. Example:

WebElement element = driver.findElement(By.xpath("//tagname[@attribute='value']"));

What is the difference between absolute and relative XPath?

Absolute XPath starts from the root node and follows the complete path to the element, while Relative XPath starts from the current node and searches for the element based on its relationship to other elements. Example:

// Absolute XPath
/html/body/div[1]/div[2]/form/input[1]/input[2]/button
// Relative XPath
//input[@name='username']

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

The findElement() method finds a single web element, while findElements() finds a list of web elements that match the criteria. Example:

WebElement element = driver.findElement(By.id("exampleId"));
List<WebElement> elements = driver.findElements(By.className("exampleClass"));

What is the difference between getText() and getAttribute() methods in Selenium?

The getText() method in Selenium is used to retrieve the visible text of an element, while the getAttribute() method is used to retrieve the value of a specified attribute. Example:

WebElement element = driver.findElement(By.id("exampleId"));
String text = element.getText();
String value = element.getAttribute("value");

What are the common exceptions you encounter in Selenium?

Some common exceptions in Selenium include:

  • NoSuchElementException: Thrown when an element could not be found.
  • TimeoutException: Thrown when a command does not complete in the specified time.
  • StaleElementReferenceException: Thrown when an element is no longer attached to the DOM.
  • ElementNotVisibleException: Thrown when an element is present in the DOM but not visible.
  • WebDriverException: Thrown when the WebDriver is unable to perform the action.
  • InvalidSelectorException: Thrown when a selector is invalid.

What are different types of waits in Selenium?

Waits in Selenium are used to pause the execution of the test script until a certain condition is met or for a specified amount of time. There are three types of waits in Selenium:

  • Implicit Wait: Sets a default wait time for the entire test script. Example:
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  • Explicit Wait: Waits for a specific condition to be met before proceeding. Example:
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("exampleId")));
  • Fluent Wait: Waits for a specific condition to be met, with the ability to set the polling frequency and ignore specific exceptions. Example:
    FluentWait wait = new FluentWait<>(driver)
              .withTimeout(Duration.ofSeconds(10))
              .pollingEvery(Duration.ofSeconds(1))
              .ignoring(NoSuchElementException.class);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("exampleId")));

What is the difference between implicit, explicit, and fluent waits in Selenium?

Implicit Wait: Sets a default wait time for the entire test script. Example:

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

Explicit Wait: Waits for a specific condition to be met before proceeding. Example:

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

Fluent Wait: Waits for a specific condition to be met, with the ability to set the polling frequency and ignore specific exceptions. Example:

FluentWait wait = new FluentWait<>(driver)
          .withTimeout(Duration.ofSeconds(10))
          .pollingEvery(Duration.ofSeconds(1))
          .ignoring(NoSuchElementException.class);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("exampleId")));

What is the difference between assert and verify commands in Selenium?

In Selenium, the assert command checks if a given condition is true. If false, the test stops execution. The verify command also checks a condition, but if false, the test continues execution. Example:

// Assert
assertEquals(expected, actual);

// Verify
if (expected.equals(actual)) {
  System.out.println("Condition is true");
} else {
  System.out.println("Condition is false");
}

What is the use of the driver.get() method in Selenium?

The driver.get() method in Selenium is used to open a specified URL in the browser. Example:

driver.get("https://nicetesters.com");

How do you handle alerts in Selenium?

In Selenium, you can handle alerts using the Alert interface. Methods include accept(), dismiss(), getText(), and sendKeys(). Example:

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

What is the difference between Selenium and QTP?

Selenium is an open-source tool that supports multiple browsers and programming languages, while QTP (QuickTest Professional) is a commercial tool that primarily supports VBScript and Internet Explorer.

How to close the current browser window in Selenium?

You can use the driver.close() method in Selenium to close the current browser window that the driver has focus on. Example:

driver.close();

How to close all browser windows in Selenium?

You can use the driver.quit() method in Selenium to close all browser windows opened by the WebDriver and safely end the session. Example:

driver.quit();

What is the difference between driver.close() and driver.quit() methods in Selenium?

The driver.close() method closes the current browser window, while the driver.quit() method closes all browser windows and ends the WebDriver session.

How to navigate to a URL, refresh, or move through browser history in Selenium?

You can use the driver.navigate() method in Selenium to navigate to a URL, refresh the page, move forward, or move backward in the browser's history. Example:

driver.navigate().to("https://nicetesters.com");
driver.navigate().refresh();
driver.navigate().back();
driver.navigate().forward();

How to manage browser properties in Selenium?

You can use the driver.manage() method in Selenium to manage browser properties such as timeouts, window size, and cookies. Example:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();

How to switch context in Selenium?

You can use the driver.switchTo() method in Selenium to switch the context to a different frame, window, or alert. Example:

driver.switchTo().frame("frameName");
driver.switchTo().window("windowName");
driver.switchTo().alert().accept();

How to find a single web element in Selenium?

You can use the driver.findElement() method in Selenium to find a single web element on the web page. Example:

WebElement element = driver.findElement(By.id("exampleId"));

How to find a list of web elements in Selenium?

You can use the driver.findElements() method in Selenium to find a list of web elements on the web page. Example:

List<WebElement> elements = driver.findElements(By.className("exampleClass"));

How to get the title of the current web page in Selenium?

You can use the driver.getTitle() method in Selenium to get the title of the current web page. Example:

String title = driver.getTitle();

What is a WebElement in Selenium?

A WebElement represents an HTML element on a web page. It provides methods to interact with the element, such as clicking, typing, and retrieving attribute values.

How do you handle dropdowns in Selenium?

You can handle dropdowns in Selenium using the Select class. Example:

Select dropdown = new Select(driver.findElement(By.id("dropdownId")));
        dropdown.selectByVisibleText("OptionText");

How do you perform mouse actions in Selenium?

You can perform mouse actions in Selenium using the Actions class. Example:

Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();

How do you handle multiple windows in Selenium?

You can handle multiple windows in Selenium using the driver.getWindowHandles() method to get the window handles and driver.switchTo().window() to switch between them. Example:

String mainWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
for (String window : allWindows) {
  driver.switchTo().window(window);
}

How to handle scrolling in Selenium?

We might have to scroll down or up to view the elements that are not visible on the screen. To handle scrolling in Selenium, we can use the below code snippet:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,500)");
If you have liked our content, please share it with your friends and colleagues.