enter image description here

See how the dynamic message displays in the browser. I am adding a name in a text field and clicking save button. After clicking save, a message displays as New Staff added or Staff Name already exists!. I want to get the content of the message. Based on the content I want to proceed with my testcase or end it. How can this be done using selenium?

  • This is not a duplicate. The message "New Staff added." is not a JavaScript alert. Its just a HTML container, containing a text. May 16, 2014 at 13:23
  • From attached image, it seems that @Niels is right and message the yellow "alert" is a dynamically created HTML element. You need to find it's id/name/css locator, whichever method to use to find those. May 16, 2014 at 13:44
  • I edited the question to call out that this isn't looking for an "alert" but a dynamic element
    – Kate Paulk
    May 16, 2014 at 14:24
  • Kate, @Niels, thanks for your persistence. An unfortunate lexical ambiguity, now resolved.
    – corsiKa
    May 16, 2014 at 17:27
  • How does the HTML source look? Is the dynamic message hidden/shown by CSS or loaded with JS? Feb 10, 2015 at 23:07

5 Answers 5


What you are trying to test feels like pretty basic automated web testing.

  1. Do some action
  2. Wait for new element to appear
  3. Verify content of element

Without the actual HTML its hard for anyone to assist you with identifying the element. Elements are identified in Selenium with different type of selectors.

I think the best advice I can give you is to get better (web testing) knowledge:

  1. Learn HTML and CSS so you understand the structure of the websites/applications, follow the free online course on Codecademy.
  2. Follow Selenium tutorials to understand basic element finding
  3. Read a good Selenium book

In chrome, when you manually go through that process, when the message pops up, right click and choose "Inspect Element". This will show you the element in the DOM. Then in your automation code you just create the element and check whether it is displayed.


It would be a good idea to use the Firebug plugin in Firefox and load it up beforehand by right clicking on a div close to where this dynamic div gets positioned - then choose "Inspect Element with Firebug" - then perform the action that is supposed to bring up this dynamic div - Firebug will highlight the change in page content for a brief moment that should help you to locate this new div - copy either its id (if set)(highest priority) or classname (if unique)(2nd highest priority) or right click and choose "Copy xpath" (if constant for multiple invocations - could be unlikely)(lowest priority). Then you should be able to apply your requisite condition by extracting .Text from this div.

I recently used this approach to check whether a dynamically generated drop-down list in my application gets displayed above or below its button (so technically a float-up or drop-down list) in a hovering dynamic div.

There is one catch though that you might want to write a wrapper over FindElement() containing a try-catch block so that the webdriver does not break your test in case it cannot find this div. Then you can add the != null condition before extracting the .Text from the div.


It will be good to do this with firepath, it works well for Firefox browser. And once you know the path you can implement the same (litle changes might be need for explorer though) for other browsers. Please check the link, it has a better explanation for locating an element


If you're using chrome, you can go into chrome developer tools and go to the source tab. From there you can pause browser execution using the pause button at the top. Wait for the notification to appear and then pause the browser so it doesn't disappear on it's own. That will let you take your time to observe the html and decide on a css selector that adequately targets it.

Once you have your css selector you can approach the problem like this:

// Lets pretend that the css selector you want is a classname called "notification"
By css = By.cssSelector(".notification");

WebDriverWait wait = new WebDriverWait(driver, 30);

// throws TimeoutException if it doesn't become present after 30 seconds.  
// Assuming it appears, it returns a WebElement for further use,
// otherwise null.
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(css));

// throws TimeoutException if it doesn't become visible after 30 seconds

// throws TimeoutException if it doesn't disappear after 30 seconds

This example will give you 30 seconds of flexibility for the element to enter different states, appearing, displaying, and disappearing. You can perform actions in-between these to deal with each specific state. Or catch the exceptions to react to them if they fail. Hope this helps!

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.