How to get HTML5 validation message with selenium?

前端 未结 2 668
無奈伤痛
無奈伤痛 2021-02-10 15:18

Please see this website.

After click log in i have this User credentials form and after try to login with missing Email Address or Password i g

2条回答
  •  时光取名叫无心
    2021-02-10 15:31

    The validation messages are not the part of your DOM. They are generated because your input fileds have required attribute. If you see the HTML of your fields -

    
    

    You can see it has required attribute turned on. Check this out. You can verify that your fields have this required attribute or not, like this-

    WebElement inputElement = driver.findElement(By.name("uname"));
    JavascriptExecutor js = (JavascriptExecutor) driver;  
    boolean isRequired = (Boolean) js.executeScript("return arguments[0].required;",inputElement)
    if(isRequired )
    {
       //element is required and validation error will popup if the field is empty.
    }
    

    There is no need to care about whether the message appears or not because that will handled by the browser.

提交回复
热议问题