我有一个包含必填字段的表单,当我尝试提交带有空白必填字段的表单时,会显示一条错误消息,如下面的屏幕截图所示。使用 Java 中的 Selenium 无法识别是否显示错误消息,因为这些错误是 HTML5 并且它们在 Dom 中不存在。所以我用谷歌搜索,发现我们可以使用 Javascript。但是当我使用它时,即使显示了消息,我每次都会变得错误。您的帮助将不胜感激。
WebElement field = driver.findElement(By.xpath('//input[@id='frstNm']'))
WebElement registerBtn = driver.findElement(By.xpath('//div//button[text()='Register']'))
registerBtn.click() Boolean is_valid = ((js.executeScript('return arguments[0].checkValidity();', field)) as Boolean)
String message = ((js.executeScript('return arguments[0].validationMessage;', field)) as String)
println(is_valid)
html:
<div class="row" style="border: solid 1px white">
<input id="prtyId" name="prtyId" type="hidden" value="">
<div class="col-xs-3 col-md-3" style="border: solid 1px white">
<div class="row">
<div class="col-xs-10 col-md-10 required">
<label for="frstNm" class="control-label">First Name</label>
<input id="frstNm" name="frstNm" onkeypress="return event.charCode < 48 || event.charCode > 57" type="text" class="form-control" required="required" value="" maxlength="50">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-7 col-md-7 required">
<label for="mi">Middle Initial</label>
<input id="mi" name="mi" onkeypress="return event.charCode < 48 || event.charCode > 57" type="text" class="form-conrol" value="" maxlength="1">
</div></div>
<br>
<div class="row">
<div class="col-xs-10 col-md-10 required">
<label for="lstNm" class="control-label">Last Name</label>
<input id="lstNm" name="lstNm" onkeypress="return event.charCode < 48 || event.charCode > 57" type="text" class="form-control" required="required" value="" maxlength="50">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-10 col-md-10 required">
<label for="suffix">Suffix</label><input id="suffix" name="suffix" onkeypress="return event.charCode < 48 || event.charCode > 57" type="text" class="form-control" value="" maxlength="5">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-10 col-md-10 required">
<label for="socialSecurity" class="control-label">SSN</label>
<input id="socialSecurity" name="socialSecurity" onkeypress="return event.charCode >= 48 && event.charCode <= 57" pattern="\d{3}-\d{2}-\d{4}" placeholder="XXX-XX-XXXX" type="text" class="form-control" required="required" value="" maxlength="11">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-10 col-md-10 required">
<label for="dateOfBirth" class="control-label">Date of Birth</label>
<input id="dateOfBirth" name="dateOfBirth" onkeypress="return event.charCode >= 48 && event.charCode <= 57" pattern="\d{2}/\d{2}/\d{4}" placeholder="MM/DD/YYYY" type="text" class="form-control" required="required" value="" maxlength="10">
</div>
</div>
<br>
<div class="row">
<div class="col-xs-7 col-md-7 required">
<label for="gender" class="control-label">Gender</label>
<select id="gender" name="gender" class="form-control" required="required">
<option value="">----Select----</option>
<option class="form-control" value="M">Male</option><option class="form-control" value="F">Female</option><option class="form-control" value="O">Other</option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="col-xs-10 col-md-10">
<label for="emailAddress">Email Address</label>
<input id="emailAddress" name="emailAddress" placeholder="mailbox@domain.tld" type="email" class="form-control" value="" maxlength="50">
</div>
</div>
<br>
</div>