When incorrect value is entered in textbox next hint is appeared:
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>
I need to get text from this hint.
I have tried to used:
Alert alert = driver.switchTo().alert()->getText();
but it did not help.
How to get text from such hints?
Advertisement
Answer
This error hint is basically the HTML5 Constraint validation message which is the outcome of Constraint API’s element.setCustomValidity() method.
To get text from the error hint you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
Using cssSelector:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#inputEmail[name='_username']"))).getAttribute("validationMessage"));
Using xpath:
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='inputEmail' and @name='_username']"))).getAttribute("validationMessage"));
References
You can find a couple of relevant detailed discussions in: