Skip to content
SDETRole
SDETRole

Practice / Selenium

Selenium Challenge Lab

Selenium interviews test whether you can write stable locators and synchronise correctly. Practise By strategies against real HTML, then fix Java snippets with broken waits, frame handling and page objects.

Challenges
13
Beginner
4
Intermediate
8
Advanced
1
Solved
0

Challenges

  1. 01 By.id or By.cssSelector? Two email inputs share name="email" and only one has an id. Scope a CSS selector to the newsletter form so findElement returns the right field.
  2. 02 Locate a button with a generated id The Submit order button's id changes on every build. Write an XPath that finds it by visible text with normalize-space(), not by id.
  3. 03 Read a value next to its label Use the XPath following-sibling axis to get the value cell next to the Status label in a details table, without relying on row positions.
  4. 04 Count the organic search results Use findElements with a precise CSS selector to collect only the organic result items, excluding the sponsored result, nested sitelinks and related searches.
  5. 05 Set up and quit the driver safely Fix a test that hard-codes a chromedriver path and leaks browsers on failure: let Selenium Manager resolve the driver and quit in @AfterMethod.
  6. 06 Replace sleeps with explicit waits Swap Thread.sleep and a mixed-in implicit wait for WebDriverWait with ExpectedConditions, waiting for a clickable button and a visible confirmation.
  7. 07 Fix a StaleElementReferenceException Sorting a table re-renders its rows, so saved WebElements go stale. Wait for staleness and re-locate the rows instead of reusing old references.
  8. 08 Fill a card form inside an iframe Switch into the payment iframe before filling the card fields, then switch back to the default content to click Pay. Avoids NoSuchElementException.
  9. 09 Switch to a new window and back A Terms link opens a new window. Store the original handle, find the new one with getWindowHandles, assert, close it and switch back.
  10. 10 Refactor into a Page Object Rewrite a LoginPage that caches WebElements and asserts internally into a clean page object: By fields, intention-revealing methods, no assertions.
  11. 11 Handle confirm and prompt dialogs Wait for native JavaScript dialogs with alertIsPresent, read their text, dismiss a confirm and answer a prompt, with no Thread.sleep.
  12. 12 Data-driven login with a TestNG DataProvider Collapse three copy-pasted login tests into one @Test fed by a @DataProvider that returns Object[][], one row per credential set.
  13. 13 Run tests on Selenium Grid with RemoteWebDriver Replace a local ChromeDriver with a RemoteWebDriver that sends ChromeOptions to a Selenium Grid 4 hub, and always quit the session in teardown.

What Selenium interviews check

Selenium WebDriver has been around long enough that interviewers assume you know the API. What they probe is judgement: can you write a locator that survives the next release, can you synchronise with the page without Thread.sleep, and can you structure a suite so that a hundred tests do not repeat the same login code? This lab is built around those three skills, in Java, the language most Selenium frameworks still use.

XPath and CSS practice against real HTML

The locator challenges give you a page fixture and a target element. You write a By strategy (By.id, By.name, By.className, By.cssSelector, By.xpath or By.linkText), and the lab resolves it with the same XPath and CSS engines a browser uses, then shows what matched. A locator must match the target exactly: matching it plus a hidden duplicate counts as a failure, as it would cause a flaky test.

Start with choosing between id and CSS and counting elements with findElements, then move to the two patterns interviews ask about most: locating a button whose id is generated, where //button[normalize-space()='Submit order'] finds it by visible text instead of an id that changes every build, and following-sibling navigation, where you read a value by its label, as in //th[normalize-space()='Status']/following-sibling::td[1], without relying on row positions.

Waits, frames, windows and framework code

The Java challenges cover the situations that make Selenium suites flaky. The canonical explicit wait looks like new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.id("submit"))).click();. Do not combine it with a non-zero implicit wait: the two interact unpredictably and can multiply timeouts, which is why the Selenium documentation warns against mixing them.

How the Java challenges are checked

Running arbitrary Java and real browsers needs an isolated execution service, which SDETRole does not operate. So the Java code challenges are validated against pattern checks: each check tests for one specific practice, such as using WebDriverWait instead of Thread.sleep or switching back with defaultContent(). They are not compiled or executed, and each check says what it looks for. The locator challenges, by contrast, are resolved for real.

Use the lab alongside the Selenium WebDriver basics lesson and the Selenium interview questions, where the model answers explain the architecture behind each pattern.

Frequently asked questions

What is Selenium WebDriver?

Selenium WebDriver is an open-source library for automating real browsers. Your test code, written in Java, Python, C#, JavaScript or Ruby, sends commands through the W3C WebDriver protocol to a browser driver such as chromedriver or geckodriver, which controls Chrome, Firefox, Edge or Safari. It automates browsers but is not a test framework, so it is usually combined with TestNG, JUnit or pytest, plus Selenium Grid for running tests in parallel across machines.

How can I practise Selenium online?

Use this lab. Locator challenges resolve your By strategy (id, name, className, cssSelector, xpath, linkText) against a real page fixture in your browser, using the same CSS and XPath engines a browser uses. Java code challenges cover waits, iframes, windows, alerts, page objects and Grid, and are checked with pattern rules rather than compiled. Pair it with the XPath and CSS selector tester to try selectors on your own HTML.

What are the most common Selenium interview questions?

Expect WebDriver architecture; locator strategies and XPath axes; implicit, explicit and fluent waits; handling StaleElementReferenceException; switching to iframes, windows and alerts; the Page Object Model; data-driven tests with a TestNG DataProvider; parallel runs with ThreadLocal drivers; and Selenium Grid 4. Senior roles add framework design and migrating a suite to Playwright. Each has a worked answer in the Selenium interview questions.

What is the difference between implicit wait and explicit wait?

An implicit wait is a global setting: every findElement call polls up to the timeout for the element to exist. An explicit wait targets one condition, for example new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(button)), and can wait for visibility, clickability, text or a custom condition. Prefer explicit waits and keep the implicit wait at zero, because mixing the two can produce unpredictable wait times. See Selenium waits explained.

How do I handle dynamic elements in Selenium?

Dynamic elements have IDs or classes that change on every load, such as id='btn-48213'. Anchor on something stable instead: a data-testid, a name, label text, or a partial match like //button[starts-with(@id,'btn-')] or [id^='btn-']. Use XPath axes to find an element relative to a stable neighbour, and pair the locator with an explicit wait for visibility or clickability. Practise on the dynamic ID XPath challenge.

What is Page Object Model in Selenium?

The Page Object Model is a design pattern where each page or component has a class that holds its locators and exposes user actions, such as loginPage.signIn(user, password). Tests call those methods instead of using locators directly, so when the UI changes you update one class rather than every test. Keep assertions in tests, return the next page object from navigation methods, and avoid giant catch-all page classes. Try the Page Object Model challenge.

How do I handle StaleElementReferenceException?

The exception means an element you found earlier is no longer attached to the DOM, usually because the page or a component re-rendered after you located it. Locate the element again just before using it, wait for the re-render to finish (for example ExpectedConditions.stalenessOf(oldElement) followed by a fresh find), and keep By locators in page objects instead of caching WebElement references. Blind retries hide the cause. Practise in the stale element challenge.

Is Selenium still relevant in 2026?

Yes. Selenium is still widely used in enterprise test suites, it is built on the W3C WebDriver standard that browser vendors support, and it keeps evolving: Selenium 4 added relative locators, a redesigned Grid and WebDriver BiDi support. Many new projects choose Playwright, so knowing both is useful. Selenium's concepts (locators, synchronisation, page objects, grids) transfer directly, and job listings in Java-heavy organisations still ask for it.

Is XPath or CSS better for Selenium?

Prefer a stable id or name, then CSS for structure and attributes because it is shorter and widely understood. Use XPath when you need to match on text or move to a parent or sibling, which CSS cannot do.

Why are the Java challenges not executed?

Compiling Java and driving a real browser requires a sandboxed execution service. Until one is available, Java challenges are checked with pattern rules and say so; locator challenges are resolved against real HTML.

Type to search across SDETRole.

Full-text search by Pagefind · runs in your browser Open search page →