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

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

问题描述

  1. 在电子邮件字段中提供无效数据时,它会提示输入 HTML 5 警报消息.
  2. Firebug 在两种情况下不允许检查此错误消息(请输入电子邮件地址):

一个.使用 Firebug 检查时,它消失了.

a. While inspecting with Firebug it is getting disappeared.

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

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

推荐答案

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

请注意,即使 validationMessage 是一个属性,也可以使用 getAttribute 来获取它:

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天全站免登陆