Selenium stale element exception – Element is no longer attached to the DOM

Posted: December 7, 2012 in web

A quick way to solve this is by adding Thread.sleep(a second or less) on the click event of an Ajax request.

Test Class:

...
    @Test
    @OperateOnDeployment(SOME_DEPLOYMENT)
    public void creditCardNumber_NotEntered_RequiredMessageDisplayed() {
        pageObject.goToPaymentForm();
	pageObject.clickCreditCard();
	pageObject.fillAllFormValues();
        pageObject.clearCreditCardField();
        pageObject.clickSubmit();
        Assert.assertEquals("Required credit card message not visible",
                EXPEDTED_CREDITCARD_REQUIRED_MESSAGE, pageObject.getActualMessage());
    }
...

Page Object:


...

@FindBy(css = ".creditCard")
private WebElement creditCardRadio;

public void clickCreditCard(){
	waitModel().withTimeout(10, TimeUnit.SECONDS).until(element(creditCardRadio).isVisible());
	creditCardRadio.click();
	
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
}
...

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s