Home > front end >  How to find prompt by Selenium
How to find prompt by Selenium

Time:08-09

Does anybody know how to find this type of element by Selenium? (to validate its presence or text)? enter image description here

I tried to catch it as alert (swithToAlert()) but it doesn't work. Any ideas? It is also can not be inspected as element and I can't find it in Elements. Thank you in advance.

CodePudding user response:

This uses HTML5 form validation. This is created by the browser, and does not exist in the DOM. Therefore Selenium cannot see this.

You can access this using JavaScript. Here is a brief code sample:

JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
WebElement field = driver.findElement(By.tagName("input"));  // your input box

if (!(Boolean) jsExecutor.executeScript("return arguments[0].validity.valid;", field)) {
        return (String) jsExecutor.executeScript("return arguments[0].validationMessage;", field);
    }

The entire API is documented.

element.validity.valid

Returns true if the element's value has no validity problems; false otherwise.

So this popup is being displayed if this returns false, but only after clicking Submit on the form.

  • Related