8

enter image description here

  1. On giving invalid data in the email field it prompts for an HTML 5 alert message.
  2. Firebug doesn't allow to inspect this error message (Please enter an email address) in two cases:

a. While inspecting with Firebug it is getting disappeared.

b. Right click on top of the error message doesn't work to inspect the element in DOM structure.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Rahul N
  • 103
  • 2
  • 7

4 Answers4

14

The Selenium API doesn't support directly a required field. However, you can easily get the state and the message with a piece of JavaScript (Java):

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 it's also possible to use getAttribute to get the validationMessage even though it's a property:

String message = driver.findElement(By.name("email")).getAttribute("validationMessage");
Florent B.
  • 41,537
  • 7
  • 86
  • 101
2

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

DeiDei
  • 10,205
  • 6
  • 55
  • 80
0

You get all html5 required field validation in this way.

String errorMessage = driver.findElement(By.id("email")).getAttribute("validationMessage");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

The HTML5 input field error message is actually the HTML5 Constraint validation message which is the outcome of Constraint API's element.setCustomValidity() method.

To retrieve the text Please enter an email address. you can use either of the locator strategies:

  • Using Java and cssSelector:

    WebElement emailField = driver.findElement(By.name("email"));
    System.out.println(emailField.getAttribute("validationMessage"));
    
  • Using Python and xpath:

    emailField = driver.find_element(By.NAME, "email")
    print(emailField.get_attribute("validationMessage"))
    

References

You can find a couple of relevant detailed discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352