selenium定位H5表单验证的提示语


  • 前端代码,1.html
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script>
             function validateFeeling(input) {
               if (input.value == " ") {
                 input.setCustomValidity('此项为必填');
                 return false;
               }
             }
    </script>

    </head>
    <body>
        <form action="" method="get">
        <input type="text" oninput="validateFeeling(this)" required />
        <input type="submit"  value="提交" />
        </form>
    </body>
    </html>
  • selenium-python
import time
    from selenium import webdriver

    driver = webdriver.Chrome()
    driver.get("http://127.0.0.1:5000/static/1.html")
    time.sleep(5)

    # 直接获取输入框中的提示文本
    print("没填写文字获取输入框中的提示文本:",driver.find_elements_by_css_selector("input")[0].get_attribute("validationMessage"))


    temp = driver.find_elements_by_css_selector("input")[0]

    # 获取验证框的true或者False的状态
    print("输入文本之前获取验证框的状态:",driver.execute_script("return arguments[0].checkValidity()", temp))

    temp.send_keys("123")

    # 获取验证框的true或者False的状态
    print("输入文本获取验证框的状态:",driver.execute_script("return arguments[0].checkValidity()", temp))


    # 直接获取输入文本之后点击提交的文本
    print("输入文本之后点击提交的文本:",driver.find_elements_by_css_selector("input")[0].get_attribute("validationMessage"))
    time.sleep(5)
    driver.quit()


原文链接:https://blog.csdn.net/baidu_39372836/article/details/96424699