Top 10 Selenium Important Interview Questions: Testing Interview Questions list 2024

1. What is Selenium?

Selenium is an open-source automation testing tool used for web application testing. It provides a way to automate browser activities such as navigating through web pages, filling forms, and interacting with web elements.

2. Explain the difference between findElement() and findElements() in Selenium WebDriver.

findElement() is used to locate the first element matching the given criteria, whereas findElements() returns a list of all elements matching the criteria. If no elements are found, findElement() will throw a NoSuchElementException while findElements() will return an empty list.

3. What are the different types of locators in Selenium WebDriver?

  • ID: Find elements by their unique ID.
  • Name: Locate elements by the value of the “name” attribute.
  • Class Name: Locate elements by the value of the “class” attribute.
  • Tag Name: Find elements by HTML tag name.
  • Link Text: Locate hyperlinks by the exact text they display.
  • Partial Link Text: Locate hyperlinks by partial text matching.
  • XPath: Powerful way of navigating XML documents and can be used to locate elements in HTML.
  • CSS Selector: Selects HTML elements based on the combination of the tag, id, class, etc.

4. What is the difference between ‘WebDriver driver = new FirefoxDriver()’ and ‘WebDriver driver = new ChromeDriver()’?

These lines of code initialize different browser instances. The first one initializes a Firefox browser instance, and the second one initializes a Chrome browser instance. Each WebDriver implementation corresponds to a specific browser.

5. Explain the difference between implicit and explicit waits.

  • Implicit Wait: It tells the WebDriver to wait for a certain amount of time before throwing NoSuchElementException.
    It is set globally for the entire script.
    driver.manage().timeouts().implicitlyWait(Duration.OfSeconds(05));
  • Explicit Wait: It is used to halt execution until a certain condition is met or a maximum timeout is reached. It is applied to a particular element.
    WebElement element = driver.findElement(By.xpath(“”));
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(element);

6. Explain the Page Object Model (POM) in Selenium.

POM is a design pattern that suggests creating an object repository for web UI elements and writing separate classes for web pages. Each page class represents a web page and contains methods to interact with the elements on that page. This approach promotes code reusability, maintainability and reduce duplication.

7. How to handle dynamic elements in Selenium?

Dynamic elements can be handled using techniques such as:

  • Explicit Wait: Wait until the element becomes stable before interacting with it.
  • XPath with functions: Use XPath functions to locate elements dynamically.

8. What is TestNG, and how is it used in Selenium?

TestNG is a testing framework for Java that facilitates the execution of tests and the generation of reports. TestNG is commonly used with Selenium to structure and manage test cases. It provides

  1. annotations for test configuration.
  2. parallel execution
  3. grouping, and sequencing.
  4. it generates Report
  5. Execute failed test classes in new build

9. How to handle multiple windows in Selenium?

To handle multiple window in selenium we have one method getWindowHandle() or getWindowHandles() by this method we will get all unique codes of each webpage has one unique number so, we get all codes of open windows.
we will store those in Set<String> windows = driver.getWindowHandles();
ArrayList<String> arr = new ArrayList<String>(windows);
driver.switchTo().window(arr.get(1));
driver.switchTo().window(arr.get(0));

using above code we can switch between the windows. like child window and parent window.

10. How to perform mouse and keyboard actions in Selenium?

To perform mouse and Keyboard actions in selenium we have Actions class. create object of Actions class and pass input as driver. call Actions Class Methods like, moveToElement, perform, build, click, dragAndDrop, release, clickAndHold.

  1. Mouse Actions: Actions a = new Actions(driver);
    a.moveToElement(element).build().perform(); // move to that element
    a.moveToElement(element).click().build().perform(); // to click
    a.moveToElement(element).doubleClick().build().perform(); // to double click
    a.moveToElement(element).ContextClick().build().perform(); // to right click
  2. Keyboard Actions: Actions a = new Actions(driver);
    a.moveToElement(Keys.DOWN).build().perform();
    a.moveToElement(Keys.ENTER).build().perform();
    a.moveToElement(Keys.UP).build().perform();
  3. Also we can handle dropdown/listbox using Actions class when there is different tagname except select then we can handle dropdown using Actions class.

Conclusion:

These questions cover a range of topics commonly encountered in Selenium interviews. Be sure to review the Selenium documentation and practice hands-on coding to strengthen your understanding. most of the time interviewer can ask practical questions like how to take screenshot, how to handle dropdown, how you will enter text without sendKeys methods.