How can I verify this type of error message that uses placeholder text, put there by javascript?

enter image description here


4 Answers 4


A Message like this will probably create a new div so, if there is really no other way to identify it, you could use something along the lines of

List<WebElement> Elements = driver.findElements(By.tagName("div"));
Boolean ElementExists = false;
for(counter = 0, counter < Elements.size(); counter++){
    if(Elements.get(counter).getText().equals("The Popup text"){
        ElementExists = true;

This iterates through every div on your site gets the displayed text and sets the Boolean true if it finds an element with the desired text. This solution is exceptionally crude (and worse, slow) but it should be able to do the job. If you'd send a link to the Site I'd look if I can find a better solution.

  • Thanks for your solution. Its working fine. Have any other easy script for this. here is the link the application: easy.com.bd Jun 19, 2016 at 3:59
  • Need to script for : 1. if all field blank. 2. if any field blank. 3. if input invalid email. Jun 19, 2016 at 5:54
  • I'm not even able to locate the WebElement so I don't think I can can provide any more elegant function. Since the whole Website seems to be written in Javascript (I've never seen so many script tags on one page in my life) I guess that there might not even a better solution. You could use static Boolean getLink(String Text){ List<WebElement> Elements = driver.findElements(By.tagName("div")); Boolean ElementExists = false; for(counter = 0, counter < Elements.size(); counter++){ if(Elements.get(counter).getText().equals(Text){ ElementExists = true; } } } to reuse it.
    – Daniel
    Jun 19, 2016 at 21:57
var myElement = driver.findElements(By.tagName("div"))
     .Where(x => x.GetProperty("value").ToString()
     .Contains("Please fill out this field"))
  1. Leave email address empty and click the sign in button
  2. View page source and find the html tag of that error. Add an explicit wait for the corresponding element tag. For explicit wait, see this link: http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
  3. Check the element text and compare with the desired error message.

All the error message on a page will have same class.

You can use that message's style class to get control of the element(message box) and then if it has to show so and so message then you can verify by using text property in IWebElement.

e.g: Message is being shown as

<span class='error-block'>Please fill out this field</span>

IWebElement errorMesg= browser.FindElement(By.ClassName("error-block"));

       public bool CompareInlineErrors(string expectedMsg)
        var errorMsg = browser.FindElements (By.TagName ("span"));
        bool IsMsgMatched = false;
        for (var i = 0; i < errorMsg.Count; i++) {
             if (errorMsg [i].GetAttribute ("class").Contains("error-block")) {
                if (errorMsg [i].Displayed) {
                    if (errorMsg [i].Text.Contains (expectedMsg)) {
                                IsMsgMatched = true;
        return IsMsgMatched;

      Assert.AreEqual(true,CompareInlineErrors("Please fill out this field."));

Before executing above, you have to make sure you are validating error for one field at a time i.e by leaving one field empty and giving input to other fields.

This can still work if each field has different error message, but if it has same message then you have to do above. Thanks, hope it 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.