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");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…