"Click Method" keeps returning "Exaleoption Stale Element Exceoption"?

The Click method returns Stale Element Exception

?

  • Sometimes the method clicks on the link: -

    public @FindBy(xpath = ".//div[@class='category_menu']//a[text()='Supercars ยป']") WebElement link_Supercars;
    
          

  • However, I often get the following exception

    : -

org.openqa.selenium.StaleElementReferenceException: deprecated reference element: element not attached to page document

Method:

    public void clickSupercarsLink() throws Exception {
    this.wait = new WebDriverWait(driver, 30);
    Boolean elementPresent = wait.until(ExpectedConditions.elementToBeClickable(link_Supercars)).isEnabled();
    try {
        if (elementPresent == true) {
            link_Supercars.click();
        }
    } catch (Exception e) {
        System.out.println("Exception! - could not click on supercars link: " + e.toString());
        throw (e);
    } finally {
    }
}

      

Any ideas on how I can improve this method?

Thank you for your help!

0


source to share


1 answer


As stated above, this exception is thrown when the WebElement you are trying to use has been updated by the application between the moment you discover it and the moment you try to interact with it.

The solution might be to find it again in your catch statement:

public void clickSupercarsLink() throws Exception {
this.wait = new WebDriverWait(driver, 30);
Boolean elementPresent = wait.until(ExpectedConditions.elementToBeClickable(link_Supercars)).isEnabled();
try {
    if (elementPresent == true) {
        link_Supercars.click();
    }
} catch (StaleElementReferenceException elementUpdated){
    link_Supercars = driver.findElement(myLocator);
    if (elementPresent == true) {
        link_Supercars.click();
    }
} catch (Exception e) {
    System.out.println("Exception! - could not click on supercars link: " + e.toString());
    throw (e);
} finally {
}

      

This issue is the reason why I am not using PageFactory but @FindBy annotation to find my elements. It is not stable if you are automating an application that has a lot of javascript and / or AJAX calls.

Hope it helps

EDIT: Explanation of how I work

I create my pageObjects by defining only locators in the class:

By myElement = By.id("myId");

      



After that, you can simply use (for example):

driver.findElement(myElement).click();

      

Still not the way I work because it's pretty hard to always figure out your expectations and then the location of the element, etc.

I created a SeleniumUtils class so I just need to write something like

SeleniumUtils.clickElement(myElement);

      

And in this method, I do whatever it takes to make sure I can click the item. wait, what can be clicked, and sometimes actions that are slightly application dependent.

I'll add a little note to say this is the way I work. I'm not saying this is ideal or anything else, but since I've defined these utilities, I rarely have to write expectations anywhere in the application that help me automate it faster.

+1


source







All Articles