Scenario-Based Questions

Estimated reading: 5 minutes 15 views

How would you locate an element if the id and class attributes are dynamic?

When attributes like id and class are dynamic and change with each page load or session, you can use advanced locators such as XPath or CSS Selectors. For example, use the contains() or starts-with() functions in XPath to match a part of the attribute value that remains constant.

  • Workaround: Inspect the element for any stable attributes or use the hierarchy of parent and child elements to construct a relative XPath. For instance, //div[contains(@id, 'staticPart')].

What approach would you take to handle multiple elements with the same class?

When multiple elements share the same class, you can use the findElements() method, which returns a list of all matching elements. Iterate through the list and apply conditions to identify and interact with the desired element based on its position, text, or any unique attribute.

  • Workaround: Combine other attributes (like tagName or text()) or use indexing to pick a specific element, e.g., elements.get(index).

How would you interact with a button that becomes clickable only after some dynamic changes on the page?

Use an explicit wait to wait for the button to meet the required conditions, such as visibility or clickability. Explicit waits like WebDriverWait combined with conditions like elementToBeClickable or visibilityOfElementLocated ensure the element is ready before interaction.

  • Workaround: If the button is blocked by overlays or spinners, wait for those elements to disappear using invisibilityOfElement.

A table contains multiple rows and columns. How would you retrieve data from the last cell of the third row?

You can use XPath to locate the specific cell by referencing the table structure. For example, the XPath //table/tbody/tr[3]/td[last()] targets the last cell of the third row.

  • Workaround: Ensure the table structure is consistent by inspecting its rows and columns. If pagination or scrolling is involved, handle it first to make the cell visible.

How would you handle a situation where a dropdown does not have a <select> tag?

For non-standard dropdowns (e.g., lists or custom div-based dropdowns), interact with the dropdown by clicking to expand it, then locate and click the desired option using XPath or CSS Selectors.

  • Workaround: Use JavaScript (executeScript()) to directly select the value if the dropdown is unresponsive to standard actions.

How would you manage file downloads in a Selenium test?

Since Selenium cannot directly handle file downloads, configure the browser (e.g., Chrome or Firefox) to automatically download files to a specific location without showing the download dialog.

  • Workaround: Set up browser preferences for file handling using ChromeOptions or FirefoxProfile. For validation, check the downloaded file in the designated folder.

What would you do if a page takes too long to load during a test?

Set a page load timeout using driver.manage().timeouts().pageLoadTimeout() to terminate the operation if the page takes too long. You can also use explicit waits to wait for specific elements to load instead of the entire page.

  • Workaround: Investigate the delay and break the test into smaller steps to pinpoint issues or set up retries.

How would you handle a scenario where an alert pops up unexpectedly?

To handle unexpected alerts, use driver.switchTo().alert() to switch focus to the alert and dismiss or accept it based on the context.

  • Workaround: Implement a try-catch block to gracefully handle NoAlertPresentException when alerts are not present.

How would you validate that a tooltip is displayed on hover?

Use the Actions class to hover over the element using moveToElement(). Then retrieve the tooltip text from the title or aria-label attribute of the element.

  • Workaround: If tooltips are dynamically added to the DOM, wait for the tooltip element to appear before validation.

How would you handle a stale element reference exception?

A stale element reference exception occurs when the DOM updates after locating an element. Re-locate the element after the DOM change to resolve this issue.

  • Workaround: Wrap the action in a retry mechanism or use explicit waits to ensure the element is stable.

How would you deal with an iframe in your test?

Switch the driver’s context to the iframe using driver.switchTo().frame() by providing the frame’s index, name, or WebElement. Once done, switch back to the default content using driver.switchTo().defaultContent().

  • Workaround: Ensure the iframe is fully loaded before switching and handle nested iframes iteratively.

What would you do if an element is not visible or clickable because it is outside the viewport?

Use JavaScript to scroll the element into view with executeScript() or adjust the browser window’s size to bring the element into view.

  • Workaround: Check for obstructions like overlays or sticky headers and handle them appropriately.

How would you handle a captcha in an automated test?

Selenium cannot automate captchas directly. Use third-party services to solve captchas or disable captcha validation in the test environment if possible.

  • Workaround: Incorporate manual intervention points or pre-generate valid sessions to bypass captchas during automation.

What would you do if a web element is partially obscured by another element?

Scroll to the element using JavaScript or interact with the parent element to bring the desired element into focus.

  • Workaround: Handle overlays, sticky headers, or pop-ups causing the obstruction by dismissing or hiding them programmatically.

How would you handle dynamically loading content during a test?

Use explicit waits to wait for the desired elements or sections of the page to load completely before interacting with them.

  • Workaround: Identify loading indicators (e.g., spinners) and wait for their invisibility using invisibilityOfElementLocated.

What would you do if clicking on a link opens a new tab, but the driver remains on the old tab?

Use driver.getWindowHandles() to retrieve all open tabs and switch to the new tab using driver.switchTo().window() with the handle of the new tab.

  • Workaround: Close unnecessary tabs and switch back to the main window as needed.

Leave a Comment

Share this Doc

Scenario-Based Questions

Or copy link

CONTENTS