当用户在Selenium中输入错误值时如何提取文本框的错误提示
How to extract the error hint of text box when user entered incorrect value in Selenium
当在文本框中输入不正确的值时,会出现下一个提示:
HTML:
<form action="/login" method="post" class="form-horizontal"> <input type="hidden">
<div class="form-group form-group-withLabel">
<input type="email" name="_username" id="inputEmail" required="" autofocus="" autocomplete="off">
<label for="inputEmail" class="inner-label">Email address</label>
</div>
<div class="form-group form-group-withLabel">
<input type="password" name="_password" id="inputPassword" placeholder="Password" required="">
<label class="inner-label" for="inputPassword">Password</label>
</div>
</form>
我需要从这个提示中获取文本。
我试过用过:
Alert alert = driver.switchTo().alert()->getText();
但没有帮助。
如何从这些提示中获取文本?
这个错误提示基本上就是HTML5 Constraint validation message which is the outcome of Constraint API's element.setCustomValidity()方法
要从错误提示中获取文本,您需要为 and you can use either of the following :
引入 WebDriverWait
使用 cssSelector:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#inputEmail[name='_username']"))).getAttribute("validationMessage"));
使用 xpath:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='inputEmail' and @name='_username']"))).getAttribute("validationMessage"));
参考资料
您可以在以下位置找到一些相关的详细讨论:
当在文本框中输入不正确的值时,会出现下一个提示:
HTML:
<form action="/login" method="post" class="form-horizontal"> <input type="hidden">
<div class="form-group form-group-withLabel">
<input type="email" name="_username" id="inputEmail" required="" autofocus="" autocomplete="off">
<label for="inputEmail" class="inner-label">Email address</label>
</div>
<div class="form-group form-group-withLabel">
<input type="password" name="_password" id="inputPassword" placeholder="Password" required="">
<label class="inner-label" for="inputPassword">Password</label>
</div>
</form>
我需要从这个提示中获取文本。
我试过用过:
Alert alert = driver.switchTo().alert()->getText();
但没有帮助。
如何从这些提示中获取文本?
这个错误提示基本上就是HTML5 Constraint validation message which is the outcome of Constraint API's element.setCustomValidity()方法
要从错误提示中获取文本,您需要为
使用 cssSelector:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#inputEmail[name='_username']"))).getAttribute("validationMessage"));
使用 xpath:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='inputEmail' and @name='_username']"))).getAttribute("validationMessage"));
参考资料
您可以在以下位置找到一些相关的详细讨论: