0
0
Selenium Javatesting~20 mins

Handling StaleElementReferenceException in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stale Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?

Consider the following Selenium Java code that tries to click a button after a page refresh:

WebElement button = driver.findElement(By.id("submitBtn"));
driver.navigate().refresh();
button.click();

What will happen when this code runs?

Selenium Java
WebElement button = driver.findElement(By.id("submitBtn"));
driver.navigate().refresh();
button.click();
AThrows StaleElementReferenceException because the element reference is no longer valid after refresh.
BThe button is clicked successfully without any exception.
CThrows NoSuchElementException because the element is not found after refresh.
DThrows TimeoutException because the click waits too long for the element.
Attempts:
2 left
💡 Hint

Think about what happens to a WebElement reference after the page reloads.

assertion
intermediate
2:00remaining
Which assertion correctly verifies handling of StaleElementReferenceException?

You want to write a test that confirms your code retries finding an element after catching a StaleElementReferenceException. Which assertion best verifies this behavior?

AassertTrue(element.isDisplayed());
BassertDoesNotThrow(() -> retryFindElementAndClick());
CassertEquals("Submit", element.getText());
DassertThrows(StaleElementReferenceException.class, () -> element.click());
Attempts:
2 left
💡 Hint

Think about what it means to handle the exception properly in your test.

🔧 Debug
advanced
2:00remaining
Identify the cause of StaleElementReferenceException in this code

Review this Selenium Java code snippet:

WebElement menu = driver.findElement(By.id("menu"));
menu.click();
WebElement submenu = driver.findElement(By.id("submenu"));
driver.navigate().refresh();
submenu.click();

Why does submenu.click() throw StaleElementReferenceException?

Selenium Java
WebElement menu = driver.findElement(By.id("menu"));
menu.click();
WebElement submenu = driver.findElement(By.id("submenu"));
driver.navigate().refresh();
submenu.click();
Asubmenu element is not clickable because it is hidden by another element.
Bmenu element click caused the submenu to disappear, so submenu is not found.
Cdriver.navigate().refresh() does not reload the page, so submenu is invalid.
Dsubmenu element was found before page refresh, so its reference is stale after refresh.
Attempts:
2 left
💡 Hint

Consider when submenu was located relative to the page refresh.

framework
advanced
2:00remaining
Which Selenium Java code snippet correctly retries after StaleElementReferenceException?

Choose the code snippet that properly retries finding and clicking an element when a StaleElementReferenceException occurs.

A
for (int i = 0; i < 3; i++) {
  try {
    driver.findElement(By.id("btn")).click();
    break;
  } catch (StaleElementReferenceException e) {
    Thread.sleep(1000);
  }
}
B
try {
  driver.findElement(By.id("btn")).click();
} catch (StaleElementReferenceException e) {
  driver.findElement(By.id("btn")).click();
}
C
while(true) {
  driver.findElement(By.id("btn")).click();
  break;
}
D
driver.findElement(By.id("btn")).click();
Thread.sleep(1000);
Attempts:
2 left
💡 Hint

Look for a retry loop with exception handling.

🧠 Conceptual
expert
2:00remaining
Why is StaleElementReferenceException common in dynamic web pages?

Choose the best explanation for why StaleElementReferenceException frequently occurs when testing dynamic web pages with Selenium.

ABecause dynamic pages block Selenium from clicking elements using JavaScript.
BBecause Selenium cannot locate elements on dynamic pages due to missing IDs.
CBecause dynamic pages reload or update DOM elements frequently, invalidating previously found WebElement references.
DBecause dynamic pages use frames which Selenium does not support.
Attempts:
2 left
💡 Hint

Think about how dynamic content affects element references.