Selenium: Top 20 Interview Questions for Experienced

Selenium Playlists

findElement is used to find the first web element matching the given locator strategy. If the element is not found, a NoSuchElementException is thrown

findElements, on the other hand, returns a list of web elements matching the given locator strategy. If no elements are found, an empty list is returned.

Selenium Grid is a tool that allows you to run Selenium tests on multiple machines in parallel
It allows you to distribute test execution across different machines, operating systems, and browsers
Selenium Grid consists of two components: the hub and the node
The hub is the central server that manages the test execution and the nodes are the machines that run the tests

Page Object Model is a design pattern used to create an object repository for web UI elements
It separates the UI elements from the test code, making the code more readable and maintainable
POM defines each web page as a separate class and the elements on that page as its members. It also provides methods to perform actions on those elements.

There are eight types of locators in Selenium:

  • id
  • name
  • classname
  • tagname
  • linktext
  • partiallinktext
  • cssselector
  • xpath

To handle pop-ups and alerts in Selenium, you can use the Alert interface provided by WebDriver. The Alert interface has the following methods:

  • accept(): Clicks the "OK" button in the alert box.
  • dismiss(): Clicks the "Cancel" button in the alert box.
  • getText(): Gets the text displayed in the alert box.
  • sendKeys(): Types the given string in the alert box.

driver.close() closes the current browser window, while driver.quit() closes all the browser windows associated with the WebDriver instance

TestNG is a testing framework for Java. It provides advanced features such as parallel test execution, test configuration, and reporting
TestNG is used in Selenium to manage test suites, test cases, and test data.

Thread.sleep() pauses the script for a specific period of time, regardless of whether the web element is available or not. WebDriverWait(), on the other hand, waits for a specific condition to be met before proceeding with the next step

To take screenshots in Selenium, you can use the TakesScreenshot interface provided by WebDriver
The interface has a method called getScreenshotAs() which takes an argument of the output type (e.g. File, Bytes) and returns the screenshot

To handle frames in Selenium, you can use the switchTo() method provided by WebDriver. The switchTo() method has the following methods to switch to the required frame:

  • switchTo().frame(int index): Switches to the frame using its index
  • switchTo().frame(String nameOrId): Switches to the frame using its name or ID
  • switchTo().frame(WebElement frameElement): Switches to the frame using the web element of the frame

Absolute XPath specifies the complete path of the element from the root node, while Relative XPath specifies the path of the element relative to the current node. Relative XPath is more reliable than Absolute XPath as it is less likely to break due to changes in the structure of the web page

To handle dynamic elements in Selenium, you can use dynamic XPath or CSS selectors. Dynamic XPath or CSS selectors use wildcards such as
*
contains()
starts-with()

You can also use the WebDriverWait() method to wait for the dynamic element to appear before performing an action

Assert and Verify are used to validate the expected and actual results of a test. Assert stops the test execution if the assertion fails, while Verify continues with the test execution even if the assertion fails

To handle dropdowns in Selenium, you can use the Select class provided by WebDriver. The Select class has the following methods:

  • selectByIndex(int index): Selects an option based on its index.
  • selectByValue(String value): Selects an option based on its value attribute.
  • selectByVisibleText(String text): Selects an option based on its visible text.

To handle mouse and keyboard events in Selenium, you can use the Actions class provided by WebDriver. The Actions class has the following methods:

  • moveToElement(WebElement toElement): Moves the mouse to the middle of the specified web element.
  • click(WebElement onElement): Clicks the specified web element.
  • sendKeys(CharSequence... keysToSend): Types the specified keys on the keyboard.

To handle multiple windows in Selenium, you can use the getWindowHandles() method to get the handles of all the open windows
You can then use the switchTo().window(String windowHandle) method to switch to the required window

DesiredCapabilities is a class in Selenium that allows you to set various properties for the WebDriver instance
It is used to specify the browser name, browser version, platform name, and other desired properties

Page Factory is a design pattern used to create page objects in Selenium
It uses the @FindBy annotation to find web elements and initializes them using the initElements() method
Page Factory makes the code more readable and maintainable

To handle SSL certificates in Selenium, you can follow these steps:

  1. Create a new Firefox profile:
    FirefoxProfile profile = new FirefoxProfile();
  2. Set the preference to accept insecure SSL certificates:
    profile.setAcceptUntrustedCertificates(true);
  3. Set the preference to ignore the SSL errors:
    profile.setAssumeUntrustedCertificateIssuer(false);
  4. Instantiate the Firefox driver with the new profile:
    WebDriver driver = new FirefoxDriver(profile);

By setting the preference to accept untrusted certificates and assuming that the certificate issuer is trusted, Selenium will bypass SSL errors and allow the test to continue.

Note that this is only recommended for testing purposes and should not be used in production environments.

Selenium: Top 20 Interview Questions for Beginners

1