Check if “Please enter an email address” message appears

前端 未结 3 1227
灰色年华
灰色年华 2020-12-15 12:15

Given a simple page:

If I enter anything in

3条回答
  •  天涯浪人
    2020-12-15 12:40

    You could try like this

    JavascriptExecutor js = (JavascriptExecutor) driver;
    driver.findElement(By.cssSelector("input[type='email']")).sendKeys("asd");
    Object s=js.executeScript("return document.getElementById(\"a\").validity.valid");
    System.out.println(s);
    driver.findElement(By.cssSelector("input[type='email']")).sendKeys("asd@gmail.com");
    s=js.executeScript("return document.getElementById(\"a\").validity.valid");
    System.out.println(s);
    

    Output for above line is

    false
    true
    

    So based on that you can conclude that whether the given value is valid or not.

    Below javascript logic will return true/false based on the validity of the field

    document.getElementById(\"a\").validity.valid
    

    P.s : I assume input tag has id "a"

提交回复
热议问题