如何使用硒获取HTML5验证消息? [英] How to get HTML5 validation message with selenium?
问题描述
请访问此网站.
单击登录"后,我具有此用户凭据表单,并尝试在缺少Email Address
或Password
的情况下登录后,收到以下消息:
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:
因此,我尝试通过打印所有页面HTML
(driver.getPageSource()
)找到该元素,但是缺少此文本.
So i try to find this element by print all the page HTML
(driver.getPageSource()
) but this text is missing.
任何想法如何验证我有这个error message
吗?
Any idea how to verify that i have this error message
?
推荐答案
验证消息不是DOM的一部分.之所以生成它们,是因为您的输入文件具有required
属性.如果您看到字段的HTML-
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(By.name("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.
这篇关于如何使用硒获取HTML5验证消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!