DevOps AI
  • Home
  • Bảo mật
  • AI & Automation
  • DevOps & Cloud
  • Bộ đề luyện thi cloud
No Result
View All Result
DevOpsAI
  • Home
  • Bảo mật
  • AI & Automation
  • DevOps & Cloud
  • Bộ đề luyện thi cloud
No Result
View All Result
DevOpsAI
No Result
View All Result
Home AI & Automation

Làm việc với Tooltip trong Selenium WebDriver

Huyen Tran by Huyen Tran
1 Tháng 5, 2025
in AI & Automation
1
Share on FacebookShare on Twitter

Tooltip là gì?

Tooltip là một element hiển thị trên màn hình dạng text, nó được dùng để chú thích hoặc là cung cấp thêm thông tin cho user. Để hiển thị tooltip, user sẽ hover chuột vào phần tử đó có tooltip và nó sẽ biến mất khi chúng ta di chuyển chuột ra khỏi phần tử đó.

Tooltip có thể hiển thị ở trên , dưới, bên trái hoặc bên phải của phần tử như sau:

Làm việc với Tooltip trong Selenium WebDriver

Có hai cách:

  1. Sử dụng phương thức getAttribute()
  2. Sử dụng class Actions

Sử dụng Phương thức getAttribute()

Dựa vào html code chúng ta có thể xác minh tooltip bằng cách lấy giá trị của thuộc tính title bằng phương thức getAttribute().

Related Post

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

16 Tháng 6, 2025
Selenium Tutorial – Java

Selenium Tutorial – Java

1 Tháng 5, 2025

Detox – gray box end-to-end testing automation framework cho ứng dụng React Native

1 Tháng 5, 2025

Cách xử lý các sự kiện bàn phím và chuột trong Selenium sử dụng class Actions

1 Tháng 5, 2025

Ví dụ:

  1. Mở trình duyệt Chrome, truy cập vào trang web devopsify.co,
  2. Click vào “Cho Phép” button để đóng pop-up “Bạn có muốn nhận cập nhật và hướng dẫn mới nhất về DevOps & Cloud?”
  3. In tooltip text của Selenium ở console
  4. Đóng trình duyệt

Lưu ý: Tai step 3, Khi bạn di chuyển chuột lên chữ Selenium, tooltip text “Tìm hiểu Selenium” được hiển thị như hình dưới.

HTML code

Như html code của element có tooltip như hình dưới, bạn sẽ thấy là tooltip text sẽ hiển thị giá trị của thuộc tính “title” của một phần tử.

Vậy cách lấy giá trị của thuộc tính title bằng phương thức getAttribute() như sau

WebElement seleniumTooltip=driver.findElement(By.linkText("Selenium"));
System.out.println("Tooltip text : "+ seleniumTooltip.getAttribute("title"));

Code example

package SeleniumWebDriverTutorial;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;



public class Tooltip {
    public static void main (String[] args) throws InterruptedException {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://devopsify.co/tao-selenium-automation-test-voi-maven-intellij/");
        Thread.sleep(3000);


        driver.findElement(By.cssSelector("button.align-right.primary.slidedown-button")).click();
        WebElement seleniumTooltip=driver.findElement(By.linkText("Selenium"));

        System.out.println("Tooltip text : "+ seleniumTooltip.getAttribute("title"));
        driver.close();

    }
}

Sử dụng class Actions

Khồng phải lúc nào tooltip cũng được lấy từ giá trị của title , nếu không có thuộc tính title vậy lúc này chúng ta không thể dùng phương thức getAttribute() để lấy giá trị tooltip được, chúng ta sẽ sử dụng class Actions., tập trung vào việc bắt chước hành động di chuyển chuột hover lên chữ Selenium của người dùng.

Class Actions được dùng để xử lý các hành động từ bàn phím hay chuột khác nhau của người dùng

Ví dụ: như hình dưới đây bạn sẽ thấy khi người dùng hover chuột luôn “Hover over me”, tooltip text ” Display tooltip” được hiển thị trên UI. Nhưng khi xem HTML code thì bạn sẽ không tìm thấy thuộc tính title.

HTML Code

Note: Bạn có thể copy và save file HTML dưới đây, thay đường dẫn đến file HTML trong code Automation Test nhé.

tooltipExampleTải xuống

Cũng giống như cách 1 sử dụng phương thức getAttribute(). Đoạn mã này chỉ cần viết lại sử dụng class Actions như sau:

  1. Xác định element có tooltip khi hover chuột lên
  2. Sử dụng Action class để thực hiện mouse hover
  3. Xác định element có chứa tooltip text
  4. In tooltip text ở console. Để lấy tooltip text, bạn sử dụng phương thức getText() nhé.

Code example

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Main {
    public static void main (String[] args) throws InterruptedException {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("D:\tooltipExample.html");
        Thread.sleep(3000);

        WebElement seleniumTooltip=driver.findElement(By.cssSelector(".tooltip"));

        //Instantiate Action Class
        Actions action = new Actions(driver);

        // Using the action class to mimic mouse hover
        action.moveToElement(seleniumTooltip).perform();
        Thread.sleep(3000);

        WebElement tooltipText=driver.findElement(By.cssSelector(".tooltiptext"));

        // To get and print the tool tip text 
        System.out.println("toolTipText-->"+  tooltipText.getText() );

        driver.close();

    }

}

Các bạn hãy thử nhé!

Tags: automation testseleniumtooltipwebdriver
Huyen Tran

Huyen Tran

Related Posts

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent
AI & Automation

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

by devopsify
16 Tháng 6, 2025
Selenium Tutorial – Java
AI & Automation

Selenium Tutorial – Java

by Huyen Tran
1 Tháng 5, 2025
Detox – gray box end-to-end testing automation framework cho ứng dụng React Native
AI & Automation

Detox – gray box end-to-end testing automation framework cho ứng dụng React Native

by Huyen Tran
1 Tháng 5, 2025
Next Post
Làm việc với Alerts/Popups trong Selenium WebDriver

Làm việc với Alerts/Popups trong Selenium WebDriver

Comments 1

  1. Pingback: Selenium Tutorial - Java - DevOpsify

Để lại một bình luận Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Recommended

Các website demo hay được sử dụng cho thực hành Automation Test

Các website demo hay được sử dụng cho thực hành Automation Test

11 Tháng 6, 2025
Cài đặt Maven trên Windows

Cài đặt Maven trên Windows

11 Tháng 6, 2025
Hướng dẫn cài đặt Kubernetes trên Ubuntu 22.04

Hướng dẫn cài đặt Kubernetes trên Ubuntu 22.04

1 Tháng 5, 2025
Tổng hợp các phiên bản Stable Diffusion: So sánh, tính năng và yêu cầu phần cứng

Tổng hợp các phiên bản Stable Diffusion: So sánh, tính năng và yêu cầu phần cứng

13 Tháng 6, 2025
Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

16 Tháng 6, 2025
MCP server 2025 tốt nhất : Hướng dẫn chọn & bảo mật

MCP server 2025 tốt nhất : Hướng dẫn chọn & bảo mật

16 Tháng 6, 2025
DevOpsify Check Tool hỗ trợ MCP – Tự động hóa kiểm tra qua AI Claude & VS Code

DevOpsify Check Tool hỗ trợ MCP – Tự động hóa kiểm tra qua AI Claude & VS Code

13 Tháng 6, 2025
GitHub Action DevOpsify Check Tool – Tự động kiểm tra bảo mật & hiệu suất

GitHub Action DevOpsify Check Tool – Tự động kiểm tra bảo mật & hiệu suất

11 Tháng 6, 2025
DevOpsify

Cộng đồng DevOps Việt Nam chia sẽ kiến thức giúp tăng tốc quá trình phát triển ứng dụng và tự động hóa trong lĩnh vực Cloud DevOps & AI.

Bài viết mới

  • Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent
  • MCP server 2025 tốt nhất : Hướng dẫn chọn & bảo mật
  • DevOpsify Check Tool hỗ trợ MCP – Tự động hóa kiểm tra qua AI Claude & VS Code

Categories

  • AI & Automation
  • Bảo mật
  • Chưa phân loại
  • DevOps & Cloud
  • Tin tức
No Result
View All Result
  • Home
  • Bảo mật
  • AI & Automation
  • DevOps & Cloud
  • Bộ đề luyện thi cloud

© 2025 DevOpsify