如何从selenium中的HTML5输入字段错误消息中获取文本? [英] How to get the text from the HTML5 input field error message in selenium?

查看:254
本文介绍了如何从selenium中的HTML5输入字段错误消息中获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 在电子邮件字段中提供无效数据时,它会提示输入HTML 5警报消息。

  2. firebug不允许检查此错误消息 - (请输入电子邮件地址)两种情况
    a。在用萤火虫进行检查时,它正在消失。

    b。右键单击错误消息顶部无法检查DOM结构中的元素。


推荐答案

Selenium API不直接支持必填字段。但是,您可以使用一段JavaScript(Java)轻松获取状态和消息:

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);

请注意,也可以使用 getAttribute 得到 validationMessage 即使它是一个属性:

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");

这篇关于如何从selenium中的HTML5输入字段错误消息中获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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