❅
❅
❆
❅
❆
❅
❆
❅
❆
❆

  • 0939206009
  • thaian.it15@gmail.com
  • Facebook
  • Youtube
  • Zalo
Anh Tester Logo
  • Khoá học
    • All Courses
    • Website Testing
    • API Testing
    • Desktop Testing
    • Mobile Testing
    • Programming Language
    • CI/CD for Tester
    • Performance Testing
  • 💥Khai giảng
  • tools
    • TestGenAI - AI Test Cases Generator
    • Mobile Apps Demo
    • Automation Framework Selenium TestNG
    • Automation Framework Cucumber TestNG
    • Gherkin Convert Functions in Katalon
    • Convert object from Selenium Java to Playwright Python
    • Website Demo CRM
    • Website Demo HRM
    • Website Demo HRM GO
    • Website Demo POS
    • Website Demo eCommerce CMS
  • blog
    • Selenium C#
    • Selenium Java
    • Katalon Tools
    • Jenkins CI/CD
    • SQL cho Tester
    • Manual Testing
    • Tài liệu cho Tester
    • Automation Testing
    • akaAT Tools
    • Cucumber TestNG
    • API Testing with Postman
    • Apache Maven
    • AI in Software Testing
    • Lịch khai giảng
  • Liên hệ
  • Log in
    Sign up

How to get HTML5 validation message with Selenium

  • Blog
  • Automation Testing
How to get HTML5 validation message with Selenium
Video

How to get HTML5 validation message with Selenium

  • Anh Tester
  • Automation Testing
  • 3475
Cách lấy giá trị TEXT Validation Message của thẻ input HTML5 bằng Selenium

1. Get HTML5 Message on input

Ví dụ có element là Email input trên website demo CMS: https://cms.anhtester.com/login

WebElement inputEmail = driver.findElement(By.xpath("//input[@id='email']"));


Bản chất của cái phần Message đó là thuộc tính đính kèm trong ô input chứ không phải là một đối tượng element rời rạc trong DOM (cây thư mục HTML) nên chúng ta không inspect để xác định nó được. Mà phải thông qua element input đó để xác định dựa trên thuộc tính của nó.

Lưu ý các thuộc tính đính kèm không hiện diện để chúng ta nhìn thấy khi inspect, mà phải tự ngầm hiểu nếu nó là thẻ HTML5 (ví dụ Bootstrap ưa dùng) thì nó sẽ tự tồn tại trong chính cái thẻ input đó, chúng ta chỉ việc chỉ định đúng tên thuộc tính để xử lý.


Để lấy giá trị text từ HTML5 validation message trong ô input ấy chúng ta sử dụng attribute name là "validationMessage", mặc dù không nhìn thấy nó xuất hiện trong DOM tại thẻ input email 😁

String validationMessage = inputEmail.getAttribute("validationMessage");
System.out.println(validationMessage);

 

Code demo sử dụng Selenium Java và TestNG Framework

@Test
public void testGetHTML5Message() {
    driver.get("https://cms.anhtester.com/login");

    driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
    sleep(1);

    WebElement inputEmail = driver.findElement(By.xpath("//input[@id='email']"));

    //Get HTML5 Message on input
    String validationMessage = inputEmail.getAttribute("validationMessage");
    System.out.println(validationMessage);

    Assert.assertEquals(validationMessage, "Please fill out this field.", "The validation message of Email field not match.");
}

 

2. Check HTML5 Has Required Validation Message

Kiểm tra xem ô input đó có tồn tại cái validation message hay không.

Sử dụng class JavascriptExecutor của Selenium để thực thi đoạn script

return arguments[0].required;

 

@Test
public void testCheckHTML5HasRequiredMessage() {
    driver.get("https://cms.anhtester.com/login");

    driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
    sleep(1);

    WebElement inputEmail = driver.findElement(By.xpath("//input[@id='email']"));

    //Check HTML5 Has Required Message
    System.out.println("Required: " + ((JavascriptExecutor) driver).executeScript("return arguments[0].required;", inputEmail));
    Assert.assertTrue((Boolean)((JavascriptExecutor) driver).executeScript("return arguments[0].required;", inputEmail), "Email not required field.");
}

 

3. Check HTML5 Message with Value Valid or Invalid

Kiểm tra xem ô input đó có chấp nhận đúng kiểu giá trị điền vào hay chưa, nếu đúng trả về true và không thông báo message nữa, còn nếu sai format giá trị thì trả về false và báo message lên.

Trong ví dụ là format của email đang điền vào ô email input.

Sử dụng class JavascriptExecutor của Selenium để thực thi đoạn script

return arguments[0].validity.valid;



@Test
public void testCheckHTML5MessageWithValueValid() {
    driver.get("https://cms.anhtester.com/login");

    driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
    sleep(1);

    WebElement inputEmail = driver.findElement(By.xpath("//input[@id='email']"));

    inputEmail.sendKeys("admin@example.com");
    sleep(2);

    //Check HTML5 Message With Value Valid - True
    System.out.println("Check Valid: " + ((JavascriptExecutor) driver).executeScript("return arguments[0].validity.valid;", inputEmail));
    Assert.assertTrue((Boolean)((JavascriptExecutor) driver).executeScript("return arguments[0].validity.valid;", inputEmail), "Email value not valid.");

    inputEmail.clear();
    inputEmail.sendKeys("admin");
    driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
    sleep(2);

    //Check HTML5 Message With Value Valid - False
    System.out.println("Check Valid: " + ((JavascriptExecutor) driver).executeScript("return arguments[0].validity.valid;", inputEmail));
    Assert.assertTrue((Boolean)((JavascriptExecutor) driver).executeScript("return arguments[0].validity.valid;", inputEmail), "Email value not valid.");
}
  • Tags:
  • Selenium
  • Tips And Tricks
  • HTML5
  • Validation Message

Chia sẻ bài viết

Facebook Linkedin Telegram Pinterest Share with Zalo Zalo

Cộng đồng Automation Testing Việt Nam

🌱 Facebook Fanpage: Anh Tester
🌱 Telegram
Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

  • Anh Tester

    Anh Tester

    Đường dẫu khó chân vẫn cần bước đi
    Đời dẫu khổ tâm vẫn cần nghĩ thấu

    • Facebook
    • Youtube
    • Zalo

Search Blogs

Related Blogs

🤖 So sánh Playwright JS/TS và Playwright Python

🤖 So sánh Playwright JS/TS và Playwright Python

Aug-29-2025 by Anh Tester
So sánh Playwright và Selenium trong Test Automation

So sánh Playwright và Selenium trong Test Automation

Aug-23-2025 by Anh Tester
🚀 Lộ trình học Automation Tester tại Anh Tester

🚀 Lộ trình học Automation Tester tại Anh Tester

Aug-23-2025 by Anh Tester
Những kỹ năng cần có cho level Senior Automation Tester

Những kỹ năng cần có cho level Senior Automation Tester

Dec-17-2024 by Anh Tester
Những kỹ năng cần có cho level Middle Automation Tester

Những kỹ năng cần có cho level Middle Automation Tester

Dec-17-2024 by Anh Tester
Những kỹ năng cần có cho level Junior Automation Tester

Những kỹ năng cần có cho level Junior Automation Tester

Dec-17-2024 by Anh Tester
Những kỹ năng cần có cho level Fresher Automation Tester

Những kỹ năng cần có cho level Fresher Automation Tester

Dec-17-2024 by Anh Tester
Cách xử lý các Exceptions trong Selenium WebDriver

Cách xử lý các Exceptions trong Selenium WebDriver

May-31-2024 by Anh Tester
Tại sao chúng ta cần Kiểm thử Tự động

Tại sao chúng ta cần Kiểm thử Tự động

May-30-2024 by Anh Tester
How to get HTML5 validation message with Selenium

How to get HTML5 validation message with Selenium

Mar-22-2024 by Anh Tester
view all

Blog Tags

  • Selenium
  • Xpath
  • Locator
  • Jenkins
  • Testing
  • Tester
  • Thuật ngữ
  • Lộ trình
  • Khóa học
  • Mindset
  • QA
  • QC
  • Checklist
  • Website
  • Mobile
  • Question
  • Answer
  • Phỏng vấn
  • Extension
  • Cucumber
  • Gherkin
  • Agile
  • Scrum
  • Document
  • Testing Level
  • Automation Test
  • Test Cases
  • Trường hợp
  • Katalon
  • JMeter
  • Postman
  • API
  • Manual Test
  • Developer

Anh Tester

Anh Tester profile
Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu

Connect me on

  • Facebook
  • Youtube
  • Zalo


Liên hệ

  • 0939206009
  • thaian.it15@gmail.com
  • Anh Tester
  • Donate for Anh Tester
QR Facebook Group
QR Discord Group

Copyright © 2021-2025 Anh Tester Automation Testing