Skip to content
QA Knowledge Hub

Selenium WebDriver quiz (Java): 20 questions with explanations

Twenty original multiple-choice questions on locators, waits, browser features and framework design in Selenium WebDriver with Java, with an explanation for every answer.

All questions are original and are not affiliated with or taken from any certification exam. Your score is worked out in your browser and saved only in your browser.

Answered 0 of 20

  1. Question 1 · Locators

    Which locator is generally the most reliable, when it is present and unique?

  2. Question 2 · Locators

    Which XPath selects a button whose visible text is "Sign in", ignoring extra spaces around it?

  3. Question 3 · Locators

    Which CSS selector matches the element with id "login"?

  4. Question 4 · Locators

    What does driver.findElements(By.className("row")) return if nothing matches?

  5. Question 5 · Locators

    Which exception does driver.findElement throw when no element matches the locator?

  6. Question 6 · Waits

    Which wait applies to every element lookup for the whole driver session?

  7. Question 7 · Waits

    Why is Thread.sleep(5000) a poor way to wait for an element?

  8. Question 8 · Waits

    Which line waits up to 10 seconds for a toast message to become visible?

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
  9. Question 9 · Waits

    A StaleElementReferenceException usually means:

  10. Question 10 · Browser features

    How do you act on an element inside an iframe?

  11. Question 11 · Browser features

    Which call returns the driver to the main page after working inside a frame?

  12. Question 12 · Browser features

    How do you accept a JavaScript alert?

  13. Question 13 · Browser features

    What is the usual way to upload a file when the page has an input of type file?

  14. Question 14 · Browser features

    Which class do you use for a native HTML select (dropdown) element?

  15. Question 15 · Browser features

    Which method moves the mouse over a menu item to reveal a sub-menu?

  16. Question 16 · Framework

    What is the main benefit of the Page Object Model?

  17. Question 17 · Framework

    Which TestNG annotation runs before every test method?

  18. Question 18 · Framework

    Tests run in parallel and share one static WebDriver field. What is the likely result?

  19. Question 19 · Framework

    What does driver.quit() do that driver.close() does not?

  20. Question 20 · Framework

    Which of these is new in Selenium 4?

20 unanswered. Unanswered questions count as wrong.

All questions with answers

Show the answer key and explanations (spoilers)
  1. Which locator is generally the most reliable, when it is present and unique?

    • Absolute XPath
    • Link text
    • id
    • Tag name

    Answer: id. A unique id is fast and does not depend on layout. Absolute XPath breaks with any change to the page structure, and tag names are rarely unique.

  2. Which XPath selects a button whose visible text is "Sign in", ignoring extra spaces around it?

    • //button[normalize-space()='Sign in']
    • //button[@text='Sign in']
    • //button.text('Sign in')
    • /button[Sign in]

    Answer: //button[normalize-space()='Sign in']. text() and normalize-space() read the element's text. There is no @text attribute in HTML, and the other two are not valid XPath.

  3. Which CSS selector matches the element with id "login"?

    • .login
    • login
    • @login
    • #login

    Answer: #login. # selects by id and . selects by class. A bare word selects a tag name.

  4. What does driver.findElements(By.className("row")) return if nothing matches?

    • It throws NoSuchElementException
    • An empty list
    • null
    • It waits for 30 seconds and then throws

    Answer: An empty list. findElements returns an empty list when there is no match, which makes it useful for checking that something is absent. Only findElement throws.

  5. Which exception does driver.findElement throw when no element matches the locator?

    • NoSuchElementException
    • StaleElementReferenceException
    • TimeoutException
    • InvalidSelectorException

    Answer: NoSuchElementException. NoSuchElementException means the lookup found nothing at that moment. InvalidSelectorException means the locator syntax itself is wrong.

  6. Which wait applies to every element lookup for the whole driver session?

    • Explicit wait
    • Fluent wait
    • Implicit wait
    • Thread.sleep

    Answer: Implicit wait. An implicit wait is set once on the driver and applies to all findElement calls. Explicit and fluent waits apply to one condition at a time.

  7. Why is Thread.sleep(5000) a poor way to wait for an element?

    • It is not allowed in Java
    • It only works in Chrome
    • It stops the browser
    • It always waits the full time and still fails if the page takes longer

    Answer: It always waits the full time and still fails if the page takes longer. A fixed sleep wastes time when the page is fast and is not enough when it is slow. A condition-based wait continues as soon as the condition is true.

  8. Which line waits up to 10 seconds for a toast message to become visible?

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    • wait.until(ExpectedConditions.alertIsPresent(By.id("toast")));
    • wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("toast")));
    • wait.sleep(By.id("toast"));
    • driver.waitFor(By.id("toast"));

    Answer: wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("toast")));. visibilityOfElementLocated polls until the element exists and is displayed. alertIsPresent is for browser alerts and takes no locator.

  9. A StaleElementReferenceException usually means:

    • The element you hold was removed or replaced in the page, so the reference is dead
    • The locator syntax is wrong
    • The browser version is too old
    • The element is hidden behind another element

    Answer: The element you hold was removed or replaced in the page, so the reference is dead. The WebElement points to a specific DOM node. After a re-render that node is gone even if an identical-looking one exists. Look the element up again.

  10. How do you act on an element inside an iframe?

    • Call findElement as usual; Selenium searches all frames
    • Use a special XPath that starts with //iframe
    • Switch to the frame with driver.switchTo().frame(...) first
    • Open the iframe URL in a new window only

    Answer: Switch to the frame with driver.switchTo().frame(...) first. Selenium looks only at the current browsing context. Switch into the frame, act, then switch back with defaultContent().

  11. Which call returns the driver to the main page after working inside a frame?

    • driver.switchTo().parent().top()
    • driver.back()
    • driver.switchTo().window()
    • driver.switchTo().defaultContent()

    Answer: driver.switchTo().defaultContent(). defaultContent() goes back to the top-level document. parentFrame() would go up only one level.

  12. How do you accept a JavaScript alert?

    • driver.findElement(By.tagName("alert")).click();
    • driver.switchTo().alert().accept();
    • driver.alert().ok();
    • driver.close();

    Answer: driver.switchTo().alert().accept();. Alerts are not part of the page DOM. Switch to the alert, then accept() or dismiss() it.

  13. What is the usual way to upload a file when the page has an input of type file?

    • Call sendKeys on that input with the absolute path of the file
    • Click the input and type the path into the operating system dialog using Selenium
    • Use the Actions class to drag the file from the desktop
    • Selenium cannot upload files

    Answer: Call sendKeys on that input with the absolute path of the file. Selenium cannot drive native operating system dialogs. Sending the file path to the input element sets the file directly.

  14. Which class do you use for a native HTML select (dropdown) element?

    • Dropdown
    • Actions
    • Select
    • WebList

    Answer: Select. The Select class wraps a select element and offers selectByVisibleText, selectByValue and selectByIndex. Custom dropdowns made of div elements are clicked like normal elements.

  15. Which method moves the mouse over a menu item to reveal a sub-menu?

    • driver.hover(element)
    • element.hover()
    • new Select(element).hover()
    • new Actions(driver).moveToElement(element).perform()

    Answer: new Actions(driver).moveToElement(element).perform(). Mouse hover, drag and drop and keyboard chords go through the Actions class. Remember to call perform().

  16. What is the main benefit of the Page Object Model?

    • It makes tests run faster
    • Locators and page actions live in one place, so a UI change is fixed once instead of in every test
    • It removes the need for waits
    • It lets Selenium test mobile apps

    Answer: Locators and page actions live in one place, so a UI change is fixed once instead of in every test. Page objects separate what the test does from how the page is operated. When a locator changes you edit one class. They do not speed up execution.

  17. Which TestNG annotation runs before every test method?

    • @BeforeMethod
    • @BeforeClass
    • @BeforeSuite
    • @Test(priority = 0)

    Answer: @BeforeMethod. @BeforeMethod runs before each @Test method, which is where you usually create a fresh driver. @BeforeClass runs once per class.

  18. Tests run in parallel and share one static WebDriver field. What is the likely result?

    • The tests run twice as fast with no side effects
    • Selenium creates a driver per thread automatically
    • Only the first test runs
    • Tests interfere with each other, clicking in each other's browsers and failing randomly

    Answer: Tests interfere with each other, clicking in each other's browsers and failing randomly. A WebDriver is not thread-safe. Give each thread its own driver, for example through ThreadLocal<WebDriver> or a per-test fixture.

  19. What does driver.quit() do that driver.close() does not?

    • Nothing, they are identical
    • It closes only the current tab
    • It ends the session, closing all windows and stopping the driver process
    • It clears the browser cache

    Answer: It ends the session, closing all windows and stopping the driver process. close() closes the window in focus. quit() ends the whole session. Always call quit() in teardown so driver processes do not pile up.

  20. Which of these is new in Selenium 4?

    • The findElement method
    • Relative locators such as above(), below() and toRightOf()
    • The WebDriver interface
    • The Select class

    Answer: Relative locators such as above(), below() and toRightOf(). Selenium 4 added relative locators (RelativeLocator.with(...)), driver.switchTo().newWindow(), and better access to browser DevTools. The other items have existed since Selenium 2 or 3.