0
0
Selenium Javatesting~10 mins

Handling StaleElementReferenceException in Selenium Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AStaleElementReferenceException
BNoSuchElementException
CTimeoutException
DElementNotVisibleException
Attempts:
3 left
💡 Hint
Common Mistakes
Using NoSuchElementException instead of StaleElementReferenceException.
Catching TimeoutException which is unrelated here.
2fill in blank
medium

Complete 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'
AfindElements
BfindElement
CgetElement
DlocateElement
Attempts:
3 left
💡 Hint
Common Mistakes
Using findElements which returns a list, not a single element.
Using non-existent methods like getElement or locateElement.
3fill in blank
hard

Fix 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'
Are-find element before clicking
Bignore exception
Cretry without re-finding element
Duse Thread.sleep before clicking
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to click the old stale element again without re-finding.
Ignoring the exception without handling.
4fill in blank
hard

Fill 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'
AfindElement
BStaleElementReferenceException
CfindElements
DNoSuchElementException
Attempts:
3 left
💡 Hint
Common Mistakes
Using findElements which returns a list instead of a single element.
Catching NoSuchElementException instead of StaleElementReferenceException.
5fill in blank
hard

Fill 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'
AfindElement
Bclick
CStaleElementReferenceException
DsendKeys
Attempts:
3 left
💡 Hint
Common Mistakes
Using sendKeys instead of click for clicking.
Not catching the correct exception type.