Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to catch the StaleElementReferenceException.
Selenium Java
try { WebElement element = driver.findElement(By.id("submit")); element.click(); } catch ([1] e) { System.out.println("Element is stale."); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using NoSuchElementException instead of StaleElementReferenceException.
Catching TimeoutException which is unrelated here.
✗ Incorrect
The StaleElementReferenceException is the exception thrown when the element is no longer attached to the DOM.
2fill in blank
mediumComplete the code to retry finding the element after catching the exception.
Selenium Java
WebElement element; try { element = driver.findElement(By.id("username")); element.sendKeys("user1"); } catch (StaleElementReferenceException e) { element = driver.[1](By.id("username")); element.sendKeys("user1"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using findElements which returns a list, not a single element.
Using non-existent methods like getElement or locateElement.
✗ Incorrect
After catching the stale element exception, you need to find the element again using findElement.
3fill in blank
hardFix the error in the retry logic to handle stale element properly.
Selenium Java
WebElement button = driver.findElement(By.id("login")); try { button.click(); } catch (StaleElementReferenceException e) { button = driver.findElement(By.id("login")); // [1] button.click(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to click the old stale element again without re-finding.
Ignoring the exception without handling.
✗ Incorrect
You must find the element again inside the catch block before clicking to avoid stale reference.
4fill in blank
hardFill both blanks to implement a retry loop that handles StaleElementReferenceException.
Selenium Java
int attempts = 0; while (attempts < 3) { try { WebElement link = driver.[1](By.linkText("Home")); link.click(); break; } catch ([2] e) { attempts++; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using findElements which returns a list instead of a single element.
Catching NoSuchElementException instead of StaleElementReferenceException.
✗ Incorrect
Use findElement to get the element and catch StaleElementReferenceException to retry.
5fill in blank
hardFill all three blanks to create a method that safely clicks an element with retries on stale reference.
Selenium Java
public void safeClick(By locator) {
int retries = 0;
while (retries < 2) {
try {
WebElement element = driver.[1](locator);
element.[2]();
break;
} catch ([3] e) {
retries++;
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using sendKeys instead of click for clicking.
Not catching the correct exception type.
✗ Incorrect
The method finds the element, clicks it, and catches StaleElementReferenceException to retry.