Write Selenium Code That Locates Two Page Elements and Returns Their Text Values
Company: Glean
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Onsite
In a QA engineer interview, the coding exercise was: write Selenium code that fetches and returns the text values of two elements on a web page. The expectation was a working version within two or three attempts, so correctness on the first runs mattered more than breadth.
```hint What "text value" means
Decide, for each element type, whether you need the rendered text or the value of a form field, and which Selenium call returns each.
```
```hint Timing
Consider what happens if one of the elements is rendered by JavaScript after the page has loaded.
```
### Clarifying Questions
- Which language binding (Python, Java, ...) and which browser?
- How are the two elements identified: id, CSS selector or XPath? Are they inside an iframe or a shadow DOM?
- Are they ordinary text elements, or form fields such as inputs whose content is a value?
- Should the function navigate to the page itself, or receive a driver that is already on it?
### What a Strong Answer Covers
- A correct locator strategy and current (Selenium 4) API usage
- Explicit waits for the elements instead of fixed sleeps
- The right kind of text for each element type, normalized
- Error handling when an element is missing, and cleanup of the browser session
- Code that can be reused and tested, not a one-off script
### Follow-up Questions
- One element's text is still empty when you read it, because it is filled in asynchronously. How do you wait for the content, not just the element?
- The element is re-rendered between locating it and reading it, and you get a stale element error. What do you do?
- How would you turn this into an assertion in a test framework?
Overview: Write Selenium code that locates two elements on a web page and returns their text values, and get it working within two or three attempts. Tests locator choice, explicit waits for dynamic content, the difference between visible text and form values, stale elements and browser cleanup.