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

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

Huyen Tran by Huyen Tran
1 Tháng 5, 2025
in AI & Automation
0
Xử lý nhiều cửa sổ trong Selenium WebDriver
Share on FacebookShare on Twitter

Đôi khi chúng ta sẽ phải làm việc trên một cửa sổ window hay nhiều cửa sổ (windows) trong dự án Automation Test cho nhiều tình huống kiểm thử khác nhau. Để có thể xử lý và kiểm soát được các hoạt động trên các cửa sổ khác nhau, chúng ta sẽ sử dụng các phương thức tích hợp của webDriver API.

Trước khi bắt đầu bài này, các bạn cần biết

  • Tạo Selenium Automation Test với maven – Intellij IDEA
  • Cách dùng WebDriverManager với Selenium
  • Window handle trong Selenium WebDriver
  • Selenium Locator

Bây giờ chúng ta bắt đầu nhé!

Các phương thức được sử dụng để xử lý nhiều cửa sổ trong Selenium

Window handle là một ID duy nhất chứa địa chỉ của tất cả các cửa sổ. Về cơ bản, đây là một con trỏ trỏ tới một cửa sổ, trả về giá trị chuỗi. Chức năng Window handle này giúp lấy các handles của tất cả các cửa sổ. Đảm bảo rằng mỗi trình duyệt sẽ có một window handle duy nhất.

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

Đây là một số phương pháp sẽ được sử dụng để handle nhiều cửa sổ trong Selenium.

String get.windowhandle()

Phương thức này sẽ giúp bạn lấy được window handle của cửa sổ hiện tại (hay ta có thể gọi là original window hoặc là cửa sổ cha – parent window).

driver.getWindowHandle();

Set<String> get.windowhandles():

Phương thức này sẽ giúp bạn lấy handle của tất cả các cửa sổ đang mở. Bạn lưu ý là Window Handle không phân biệt tab hay window nhé, nó chỉ biết hiện tại có bao nhiêu cửa số và các tab đang mở.

Chúng ta cũng sẽ sử dụng phương thức Set để chứa tất cả các window handle theo kiểu String nhé

WebDriver.TargetLocator switchTo()

 Phương thức này sẽ giúp chúng ta di chuyển qua lại giữa các cửa sổ.

driver.switchTo().window(target_window);

Kịch bản xử lý nhiều cửa sổ

Scenario:

  1. Truy cập vào trang chủ của website devopsify.co (Đây là cửa sổ cha – parent window)
  2. Mở tab mới và truy cập vào trang Automation Test (Đây là cửa sổ con thứ 1 – child window)
  3. Mở cửa sổ mới và truy cập vào trang Tạo Selenium Automation Test với maven – Intellij IDEA (Đây là cửa sổ con thứ 2 – child window)
  4. Từ cửa sổ cha, chúng ta sẽ di chuyển qua các cửa sổ con (child windows) để thực hiện các bước kiểm thử, sau đó chúng ta quay lại cửa sổ cha (parent window).

Các bước thực thi:

  1. Lấy handle của cửa sổ cha (parent window) bằng lệnh – driver.getWindowHandle(); Và in nó ra.
  2. Tuần tự mở các cửa sổ con (child windows)
  3. Lấy handles của tất cả các cửa sổ đang được mở đó bằng lệnh – driver.getWindowHandles();
  4. Sử dụng phương thức SwitchTo() để di chuyển qua lại giữa các cửa sổ.

Có nhiều cách code để thực thi kịch bản trên như sau:

Cách 1 – Sử dụng Iterator

package SeleniumWebDriverTutorial;

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

import java.util.Iterator;
import java.util.Set;

public class MultipleWindowHandlesExample {
    public static void main(String[] args) throws InterruptedException {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();

        //Navigate to https://devopsify.co/
        driver.get("https://devopsify.co/");//parent window
        String parent_window=driver.getWindowHandle(); // Returns ID of single browser window

        //Open new tab and navigate to Automation Test child page
        driver.switchTo().newWindow(WindowType.TAB);
        driver.navigate().to("https://devopsify.co/test/automation-test/");
        Thread.sleep(3000);

        //Open new window and navigate to Tạo Selenium Automation Test với maven – Intellij IDEA child page
        driver.switchTo().newWindow(WindowType.WINDOW);
        driver.navigate().to("https://devopsify.co/tao-selenium-automation-test-voi-maven-intellij/");
        Thread.sleep(3000);

        //Get all ID's of Multiple open browser windows and store in Set
        Set<String> windwIDs = driver.getWindowHandles();

        //1. Using Iterator

	Iterator it = windwIDs.iterator();
        while(it.hasNext())
        {
            String child_window=it.next();
            if(!parent_window.equals(child_window))
            {
                driver.switchTo().window(child_window);
                System.out.println(driver.switchTo().window(child_window).getTitle());
                driver.close();
            }
        }
        //switch to the parent window
        System.out.println(driver.switchTo().window(parent_window).getTitle());
    }
}

Cách 2 – Sử dụng for each loop

package SeleniumWebDriverTutorial;

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

import java.util.Set;

public class MultipleWindowHandlesExample {
    public static void main(String[] args) throws InterruptedException {
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();

        //Navigate to https://devopsify.co/
        driver.get("https://devopsify.co/");//parent window
        String parent_window=driver.getWindowHandle(); // Returns ID of single browser window

        //Open new tab and navigate to Automation Test child page
        driver.switchTo().newWindow(WindowType.TAB);
        driver.navigate().to("https://devopsify.co/test/automation-test/");
        Thread.sleep(3000);

        //Open new window and navigate to Tạo Selenium Automation Test với maven – Intellij IDEA child page
        driver.switchTo().newWindow(WindowType.WINDOW);
        driver.navigate().to("https://devopsify.co/tao-selenium-automation-test-voi-maven-intellij/");
        Thread.sleep(3000);

        //Get all ID's of Multiple open browser windows and store in Set
        Set<String> windwIDs = driver.getWindowHandles();

       
        for(String handle:windwIDs)  
        { if(!parent_window.equals(handle)){
                driver.switchTo().window(handle);
                System.out.println(driver.switchTo().window(handle).getTitle());
                driver.close();
            }
        }
        //switch to the parent window
        System.out.println(driver.switchTo().window(parent_window).getTitle());
        driver.quit();
    }
}

Cheers! 🙂

Tags: automation testseleniumtabswebdriverwindow
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 Notifications popup sử dụng Selenium Webdriver

Làm việc với Notifications popup sử dụng 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
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