I need to verfiy the dialog box displayed on clicking a 'Save' button for below conditions.

  1. Whether the dialog box is displayed?
  2. Text present in the dialog box?

I have tried using all locators and finally using css selector as below.

driver.findElement(By.cssSelector("#AdminMainContent_AlertBoxMER1_lbtnOK > span")).isDisplayed()

//to verfiy whether dilaog box is displayed

and i got exception as below

org.openqa.selenium.StaleElementReferenceException: Element is no longer valid (WARNING: The server did not provide any stacktrace information)

2 Answers 2


It sound like you need to do an explicit check on the DOM because the page state is changing after you it the save button. You will need to recheck the DOM for the element post save. You could try something like this:

            int count = 0;
        while (!driver.FindElement(By.CssSelector("#AdminMainContent_AlertBoxMER1_lbtnOK > span")).Displayed || count < 50)


I would also suggest that you take a look at the following answer on SO https://stackoverflow.com/questions/5709204/random-element-is-no-longer-attached-to-the-dom-staleelementreferenceexception.

Note: the code above is in c#


Hi guys i got it working when i changed the code as below

new WebDriverWait(driver, 100).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("AdminMainContent_AlertBoxMER1_lblMsg")));

//waiting for the alert box to get displayed System.out.println(driver.findElement(By.id("AdminMainContent_AlertBoxMER1_lblMsg")).getText());

//displaying the text in the alert box

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.