How to get text from error message in HTML5 input field in Selenium?

enter image description here

  1. When you enter invalid data in the email field, an HTML 5 warning message is requested.
  2. Firebug does not allow checking this error message (Please enter an email address) in two cases:

a. When checking with Firebug, it disappears.

b. Right-clicking at the top of the error message does not work to validate an element in the DOM structure.

+6
source share
3 answers

The Selenium API does not support the direct required field. However, you can easily get the state and message using the JavaScript (Java) part:

JavascriptExecutor js = (JavascriptExecutor)driver; WebElement field = driver.findElement(By.name("email")); Boolean is_valid = (Boolean)js.executeScript("return arguments[0].checkValidity();", field); String message = (String)js.executeScript("return arguments[0].validationMessage;", field); 

Note that you can also use getAttribute to get validationMessage , although this property:

 String message = driver.findElement(By.name("email")).getAttribute("validationMessage"); 
+10
source

In Python:

 self.browser.get(self.live_server_url + registration_path) email_input = self.browser.find_element_by_id('id_email_input') validation_message = email_input.get_attribute("validationMessage"); 

http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement

+1
source

This way you get all the html5 fields you need to verify.

 String errorMessage = driver.findElement(By.id("email")).getAttribute("validationMessage"); 
0
source

All Articles