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?
WebElement button = driver.findElement(By.id("submitBtn"));
driver.navigate().refresh();
button.click();Think about what happens to a WebElement reference after the page reloads.
After refreshing the page, the previously found WebElement becomes stale because the DOM has been reloaded. Trying to interact with it causes a 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?
Think about what it means to handle the exception properly in your test.
assertDoesNotThrow confirms that the retry method handles the exception internally and succeeds without throwing it to the test.
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?
WebElement menu = driver.findElement(By.id("menu")); menu.click(); WebElement submenu = driver.findElement(By.id("submenu")); driver.navigate().refresh(); submenu.click();
Consider when submenu was located relative to the page refresh.
The submenu element was located before the page refresh. After refresh, the DOM is new and the old reference is stale, causing the exception.
Choose the code snippet that properly retries finding and clicking an element when a StaleElementReferenceException occurs.
Look for a retry loop with exception handling.
Option A retries up to 3 times, catching the exception and waiting before retrying, which is a good practice.
Choose the best explanation for why StaleElementReferenceException frequently occurs when testing dynamic web pages with Selenium.
Think about how dynamic content affects element references.
Dynamic pages often update or reload parts of the DOM, making previously found WebElement references invalid and causing StaleElementReferenceException.