KatInterview

Fault-Tolerant Selenium Text Scraper for Dynamic Pages

System DesignModel answer
MediumModel answerSystem Designseleniumbrowser-automationexplicit-waits

Last updated

Write a Python function that waits for two CSS-selected elements to become visible on a JS-rendered page and returns their text, with None for any that time out.

Background

Modern web pages often render their content asynchronously. A navigation bar might appear instantly, while a user profile panel loads after an API call completes. Automation code that reads elements immediately after page load will fail intermittently — not always, which makes the bugs hard to catch.

Your job is to write a function that handles this reality gracefully.

Task

Implement get_element_texts(driver, selector_1, selector_2) in Python.

The function accepts a live Selenium WebDriver instance and two CSS selector strings. It should locate the first matching element for each selector, wait until it is both present in the DOM and visible on screen, then return its text content. If an element does not become visible within 5 seconds, return None for that element instead of raising an exception.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def get_element_texts(driver, selector_1, selector_2):
    # your implementation here
    pass

Requirements

  • Use explicit waits. Do not use time.sleep() anywhere in your solution.
  • The two elements must be handled independently. A timeout on one must not prevent the other from being attempted.
  • Treat an element that exists in the DOM but is not visible the same as an element that is absent — both should produce None.
  • When a selector matches more than one element, use the first match in document order.

Example

selector_1 = "#page-heading"
selector_2 = "#account-display-name"

The heading is present immediately. The account name appears after a 2-second AJAX call.

Expected return value: ["Welcome back!", "Ada Lovelace"]

Edge Cases to Handle

ScenarioExpected return value for that slot
Element appears immediatelyIts text string
Element appears via AJAX within 5 sIts text string
Element is in the DOM but hiddenNone
Element never appears within 5 sNone
Element is visible but has empty text"" (not None)
Selector matches multiple elementsText of the first match
DOM re-renders while reading .textRetry transparently, do not crash

Be prepared to discuss how you would extend this to handle a list of N selectors, and how the timeout strategy should change in that case.

Hints

Hint 1

WebDriverWait polls a callable until it returns a truthy value or the deadline passes. Think about what your polling condition should return — and remember that an empty string is falsy in Python, which can cause a subtle bug if you return el.text directly.

Hint 2

Wrap each element lookup in its own try/except TimeoutException block. This keeps the two waits fully independent: a failure on selector_1 never skips selector_2.

Hint 3

Between the moment WebDriverWait decides an element is visible and the moment your code reads .text, the DOM could re-render, leaving you with a stale reference. Adding StaleElementReferenceException to the wait's ignored_exceptions list turns that race condition into a transparent retry.