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

Windows handle trong Selenium WebDriver

Huyen Tran by Huyen Tran
1 Tháng 5, 2025
in AI & Automation
0
Windows handle trong Selenium WebDriver
Share on FacebookShare on Twitter

Window handle trong Selenium webDriver là gì?

WebDriver không phân biệt giữa windows và tabs. Nếu trang web của bạn mở một tab mới hoặc cửa sổ (window) mới , Selenium sẽ cho bạn làm việc với nó qua window handle. Mỗi cửa sổ (window) là một mã định danh duy nhất trong một phiên duy nhất. Về cơ bản, đây là một con trỏ tới một cửa sổ, trả về giá trị chuỗi (string).

Chúng ta có thể lấy mã đinh danh ID của từng window bằng cách sử dụng các phương thức do Selenium Webdriver cung cấp và sau đó sử dụng ID để chuyển sang các cửa sổ được chỉ định.

Đây là các phương thức khác nhau được cung cấp bởi Selenium WebDriver để xử lý các cửa sổ (windows)

Get window handle

Lấy window handle của cửa sổ hiện tại.

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
driver.getWindowHandle();

Di chuyển giữa các window và các tab với nhau

Khi bạn nhấp vào liên kết (link) mở ra cửa sổ mới hoặc tab mới trên màn hình, lúc này WebDriver sẽ không biết được cửa sổ nào mà Hệ điều hành cho là đang hoạt động. Để làm việc với cửa sổ mới, bạn sẽ cần chuyển sang cửa sổ đó.

Ví dụ:

Nếu bạn chỉ có hai tab hoặc cửa sổ đang mở và bạn biết mình bắt đầu với cửa sổ nào, bằng quá trình loại bỏ, bạn có thể lặp qua cả hai cửa sổ hoặc tab mà WebDriver có thể nhìn thấy và chuyển sang cửa sổ hoặc tab không phải là bản gốc.

  1. Xác định cửa sổ hiện tại xem như là bản gốc – originalWindow
  2. Kiểm tra và đảm bảo chỉ có một cửa sổ đang được mở
  3. Nhấp vào link liên kết để mở cửa sổ mới/ tab mới
  4. Di chuyển qua cửa sổ mới bằng cách:
    • Lấy danh sách các cửa sổ (windowHandles) đang mở
    • Sử dụng vòng lặp qua các window Handles đó, nếu cửa sổ/tab đó không phải là cửa sổ gốc (originalWindow) thì sử dụng lệnh swithTo() để di chuyển webDriver đến cửa sổ đó.
//Store the ID of the original window
String originalWindow = driver.getWindowHandle();

//Check we don't have other windows open already
assert driver.getWindowHandles().size() == 1;

//Click the link which opens in a new window
driver.findElement(By.linkText("new window")).click();

//Wait for the new window or tab
wait.until(numberOfWindowsToBe(2));

//Loop through until we find a new window handle
for (String windowHandle : driver.getWindowHandles()) {
    if(!originalWindow.contentEquals(windowHandle)) {
        driver.switchTo().window(windowHandle);
        break;
    }
}

Tạo mới một window hoặc một tab

Khác với việc người dùng nhấp vào link liên kết để mở một cửa sổ hoặc 1 tab mới mà webDriver không tự chuyển qua cửa sổ mới đó. Thì Selenium version 4 cung cấp API newWindow() cho phép tạo mới một cửa sổ hoặc tab và webDriver sẽ được chuyển qua cửa sổ mới đó một cách tư động.

// Opens a new tab and switches to new tab
driver.switchTo().newWindow(WindowType.TAB);

// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);

Đóng window hoặc tab

Khi bạn xong trên một cửa sổ hoặc tab và nó không phải là cửa sổ hoặc tab cuối cùng được mở trong trình duyệt của bạn, bạn nên đóng nó và chuyển về cửa sổ bạn đang sử dụng trước đó.

Việc quên chuyển về cửa sổ đang mở khác sau khi đóng cửa sổ hiện tại sẽ làm cho WebDriver thực thi trên cửa sổ hiện đã bị đóng, do đó webDriver sẽ không tìm thấy window cần làm và sẽ trả về “No Such Window Exception“. Do đó, bạn phải chuyển về cửa sổ hợp lệ để tiếp tục thực hiện kiểm thử

//Close the tab or window
driver.close();

//Switch back to the old tab or window
driver.switchTo().window(originalWindow);

Thoát khỏi trình duyệt sau mỗi phiên làm việc

Khi bạn kết thúc phiên trình duyệt, bạn nên gọi hàm driver.quit(), thay vì driver.close()

driver.quit();

Window management

WebDriver có các phương thức sau hỗ trợ việc quản lý cửa sổ trong quá trình làm việc.

Lấy kích thước của cửa sổ (get window size)

Tìm nạp kích thước của cửa sổ trình duyệt bằng pixel.

//Access each dimension individually
int width = driver.manage().window().getSize().getWidth();
int height = driver.manage().window().getSize().getHeight();

//Or store the dimensions and query them later
Dimension size = driver.manage().window().getSize();
int width1 = size.getWidth();
int height1 = size.getHeight();

Thiết lập kích thước cho cửa sổ (Set window size)

Khôi phục và thiết lập kích thước cửa sổ.

driver.manage().window().setSize(new Dimension(1024, 768));

Lấy vị trí của cửa sổ (get window position)

Tìm tọa độ của tọa độ trên cùng bên trái của cửa sổ trình duyệt.

// Access each dimension individually
int x = driver.manage().window().getPosition().getX();
int y = driver.manage().window().getPosition().getY();

// Or store the dimensions and query them later
Point position = driver.manage().window().getPosition();
int x1 = position.getX();
int y1 = position.getY();

Thiết lập vị trí cho cửa sổ (Set window position)

Di chuyển cửa sổ đến vị trí đã chọn.

driver.manage().window().setPosition(new Point(0, 0));

Phóng to cửa sổ (Maximize window)

Phóng to cửa sổ. Đối với hầu hết các hệ điều hành, cửa sổ sẽ lấp đầy màn hình mà không chặn các menu và thanh công cụ của hệ điều hành

driver.manage().window().maximize();

Thu nhỏ cửa sổ (Minimize window )

Thu nhỏ cửa sổ hiện tại. Hành vi chính xác của lệnh này dành riêng cho từng window. Minimize Window thường ẩn cửa sổ trong khay hệ thống.

Lưu ý: Tính năng này hoạt động từ Selenium version 4

driver.manage().window().minimize();

Fullscreen window

Khác với Maximize window, tính năng này lấp đầy toàn bộ màn hình, tương tự như khi bạn nhấn F11 trong trình duyệt.

driver.manage().window().fullscreen();

Code Example

package SeleniumWebDriverTutorial;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;

public class WindowHandleExample {
    public static void main(String[] args) throws Exception {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://devopsify.co");
        Thread.sleep(3000);
        //get current Window
        String originalWindow = driver.getWindowHandle();
        System.out.println("originalWindow ->  " + originalWindow);

        assert driver.getWindowHandles().size() == 1;

        // Opens a new tab and switches to new tab
        driver.switchTo().newWindow(WindowType.TAB);
        String theSecondWindow = driver.getWindowHandle();
        driver.navigate().to("https://devopsify.co/tao-selenium-automation-test-voi-maven-intellij/");
        System.out.println("theSecondWindow ->  " + theSecondWindow);


        // Opens a new window and switches to new window
        driver.switchTo().newWindow(WindowType.WINDOW);
        String theThirdWindow = driver.getWindowHandle();
        driver.navigate().to ("https://devopsify.co/test/automation-test/");
        driver.manage().window().fullscreen();
        Thread.sleep(3000);
        driver.manage().window().minimize();

        //set window size
        driver.manage().window().setSize(new Dimension(1024, 768));
        System.out.println("theThirdWindow ->  " + theThirdWindow);

        //get window size
        System.out.println("width = "+ driver.manage().window().getSize().width);
        System.out.println("heigh = "+ driver.manage().window().getSize().height);

        //get window position
        System.out.println("x = "+ driver.manage().window().getPosition().getX());
        System.out.println("y = "+ driver.manage().window().getPosition().getY());

        //Loop through three windows and tab
        for (String windowHandle : driver.getWindowHandles()) {
            driver.switchTo().window(windowHandle);
            System.out.println(driver.getTitle());
            Thread.sleep(3000);
        }

        driver.switchTo().window(originalWindow);
        driver.close();
        
        System.out.println("After closing original window");
        //Loop through three windows and tab
        for (String windowHandle : driver.getWindowHandles()) {
            driver.switchTo().window(windowHandle);
            System.out.println(windowHandle);
            Thread.sleep(3000);
        }

        driver.switchTo().window(theSecondWindow);

        driver.quit();

    }
}

Các bạn thử chạy chương trình trên để xem kết quả Selenium WebDriver xử lý các cửa sổ của trình duyệt thế nào nhé!

Cheer! 🙂

Tags: automation testseleniumwebdriverwindow
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
Xử lý nhiều cửa sổ trong Selenium WebDriver

Xử lý nhiều cửa sổ trong Selenium WebDriver

Để 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
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
Cài đặt Maven trên Windows

Cài đặt Maven trên Windows

11 Tháng 6, 2025
Cài đặt Grafana – Loki – Promtail monitoring log Container

Cài đặt Grafana – Loki – Promtail monitoring log Container

1 Tháng 5, 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