如何使用硒获取HTML5验证消息? [英] How to get HTML5 validation message with selenium?

查看:128
本文介绍了如何使用硒获取HTML5验证消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请访问网站.

单击登录"后,我具有此用户凭据表单,并尝试在缺少Email AddressPassword的情况下登录后,收到以下消息:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆