How to get HTML5 validation message with selenium?


Please see this website.

After click log in i have this User credentials form and after try to login with missing Email Address or Password i got this message:

enter image description here

So i try to find this element by print all the page HTML (driver.getPageSource()) but this text is missing.

Any idea how to verify that i have this error message ?


The validation messages are not the part of your DOM. They are generated because your input fileds have required attribute. If you see the HTML of your fields –

<input type="text" placeholder="Enter Username" name="uname" required="">

You can see it has required attribute turned on. Check this out. You can verify that your fields have this required attribute or not, like this-

WebElement inputElement = driver.findElement("uname"));
JavascriptExecutor js = (JavascriptExecutor) driver;  
boolean isRequired = (Boolean) js.executeScript("return arguments[0].required;",inputElement)
if(isRequired )
   //element is required and validation error will popup if the field is empty.

There is no need to care about whether the message appears or not because that will handled by the browser.

Answered By – Shivam Mishra

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published