
- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境設定
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - 操作命令
- Selenium - 訪問器命令
- Selenium - 斷言命令
- Selenium - 斷言/驗證方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右鍵單擊
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和標籤頁
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 iframe
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 鍵向上/向下
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援特性
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 雜項
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven 和 Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium 雜項概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium WebDriver - 視窗和標籤頁
Selenium Webdriver 可用於處理視窗和標籤頁。Selenium Webdriver 處理視窗和標籤頁沒有區別。每個開啟的視窗都有一個該會話的唯一識別符號。
開啟新視窗後,驅動程式上下文仍然保留在父視窗上。為了在子視窗上執行某些任務,我們需要將驅動程式上下文從主視窗切換到子視窗。
在 Selenium 中處理視窗和標籤頁的基本方法
Selenium 中有多種方法可用於根據視窗和標籤頁自動化測試。要訪問子視窗,我們必須首先使用 switchTo().window("子視窗的視窗控制代碼 ID") 方法將驅動程式上下文切換到另一個視窗。處理視窗和標籤頁的方法如下所示:
- driver.getWindowHandle() - 用於獲取焦點視窗的視窗控制代碼 ID。其返回型別為字串。
- driver.getWindowHandles() - 用於獲取已開啟的所有視窗的視窗控制代碼 ID。其返回型別為字串集。
示例 1
讓我們以以下頁面為例,我們將點選新標籤頁按鈕。

點選新標籤頁後,我們將導航到另一個包含文字新標籤頁的標籤頁。

程式碼實現
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.Set; import java.util.concurrent.TimeUnit; public class Tabs { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Opening the webpage where we will open a new tab driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php"); // click link and navigate to next tab WebElement b = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/button[1]")); b.click(); // Get the window handle of the original window String oW = driver.getWindowHandle(); // get all opened windows handle ids Set<String> windows = driver.getWindowHandles(); // Iterating through all window handles for (String w : windows) { if(!oW.equalsIgnoreCase(w)) { // switching to child tab driver.switchTo().window(w); // accessing element in new tab WebElement e = driver.findElement (By.xpath("/html/body/main/div/div/h1")); System.out.println("Text in new tab is: " + e.getText()); break; } } // quitting the browser driver.quit(); } }
輸出
Text in new tab is: New Tab Process finished with exit code 0
在上面的示例中,我們捕獲了新標籤頁上的文字,並在控制檯中收到了訊息 - 新標籤頁中的文字為:新標籤頁。
最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。
示例 2
讓我們再舉一個例子,如下面的圖片所示,我們將點選新視窗訊息按鈕。

點選新視窗訊息後,我們將導航到另一個包含文字新視窗訊息的視窗。

然後我們將關閉新視窗並切換回原始視窗,並在那裡獲取文字 - 瀏覽器視窗。最後,我們將退出會話。

程式碼實現
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.Set; import java.util.concurrent.TimeUnit; public class Windows { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Opening the webpage where we will open a new window driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php"); // click link and navigate to next window WebElement b = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/button[3]")); b.click(); // Get the window handle of the original window String oW = driver.getWindowHandle(); // get all opened windows handle ids Set<String> windows = driver.getWindowHandles(); // Iterating through all window handles for (String w : windows) { if(!oW.equalsIgnoreCase(w)) { // switching to child window driver.switchTo().window(w); // accessing element in new window WebElement e = driver.findElement (By.xpath("/html/body/main/div/div/h1")); System.out.println("Text in new window is: " + e.getText()); driver.close(); break; } } // switching to parent window driver.switchTo().window(oW); // accessing element in parent window WebElement e1 = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/h1")); System.out.println("Text in parent window is: " + e1.getText()); // quitting the browser session driver.quit(); } }
輸出
Text in new window is: New Window Message Text in parent window is: Browser Windows
在上面的示例中,我們捕獲了新視窗上的文字,並在控制檯中收到了訊息 - 新視窗中的文字為:新視窗訊息。然後我們關閉了子視窗並切換回父視窗。最後,在控制檯中獲取父視窗上的文字 - 父視窗中的文字為:瀏覽器視窗。
close() 和 quit() 方法的區別
close() 和 quit() 方法之間存在區別。close() 方法僅關閉焦點瀏覽器視窗,而 quit() 方法關閉與驅動程式會話相關的所有內容(瀏覽器和所有後臺驅動程式程序)。
示例 1 - 使用 Selenium 4 開啟新視窗
Selenium 4 提供了開啟新標籤頁或視窗的選項。要開啟新視窗,我們將藉助以下方法:
driver.switchTo().newWindow(WindowType.WINDOW)
讓我們再舉一個例子,我們將首先開啟一個視窗並啟動一個包含文字 - 複選框的應用程式。

然後我們將開啟一個新視窗並啟動另一個包含文字 - 單選按鈕的應用程式。

程式碼實現
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.WindowType; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class NewWindow { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Opening the webpage in window 1 driver.get("https://tutorialspoint.tw/selenium/practice/check-box.php"); // get text in window 1 WebElement e = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/h1")); System.out.println("Text is: " + e.getText()); // Initiate the Webdriver WebDriver newDriver = driver.switchTo().newWindow(WindowType.WINDOW); // Opening the webpage in new window driver.get("https://tutorialspoint.tw/selenium/practice/radio-button.php"); // get text in window 2 WebElement e1 = driver.findElement (By.xpath("/html/body/main/div/div/div[2]/form/h1")); System.out.println("Text in new window is: " + e1.getText()); // quitting the browser session driver.quit(); } }
輸出
Text is: Check Box Text in new window is: Radio Button
在上面的示例中,我們捕獲了第一個視窗中的文字,並在控制檯中收到了訊息 - 文字為:複選框。然後開啟另一個視窗並在那裡開啟一個應用程式。最後,我們在控制檯中獲取了新視窗中的文字 - 新視窗中的文字為:單選按鈕。
示例 2 - 使用 Selenium 4 開啟新標籤頁
Selenium 4 提供了開啟新標籤頁或視窗的選項。要開啟新標籤頁,我們將藉助以下方法:
driver.switchTo().newWindow(WindowType.TAB)
讓我們再舉一個例子,我們將首先開啟一個視窗並啟動一個包含文字 - 複選框的應用程式。

然後我們將開啟一個新標籤頁並啟動另一個包含文字 - 單選按鈕的應用程式。

在 NewTabs.java 類檔案上的程式碼實現。
package org.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.WindowType; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class NewTabs { public static void main(String[] args) throws InterruptedException { // Initiate the Webdriver WebDriver driver = new ChromeDriver(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // Opening the webpage in window 1 driver.get("https://tutorialspoint.tw/selenium/practice/check-box.php"); // get text in window 1 WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1")); System.out.println("Text is: " + e.getText()); // Initiate the Webdriver WebDriver newDriver =driver.switchTo().newWindow(WindowType.TAB); // Opening the webpage in new tab driver.get("https://tutorialspoint.tw/selenium/practice/radio-button.php"); // get text in other tab WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1")); System.out.println("Text in other tab is: " + e1.getText()); // quitting the browser session driver.quit(); } }
輸出
Text is: Check Box Text in other tab is: Radio Button
在上面的示例中,我們捕獲了第一個視窗中的文字,並在控制檯中收到了訊息 - 文字為:複選框。然後開啟另一個標籤頁並在那裡開啟一個應用程式。最後,我們在控制檯中獲取了新標籤頁中的文字 - 其他標籤頁中的文字為:單選按鈕。
結論
這總結了我們關於 Selenium WebDriver 視窗和標籤頁教程的全面內容。我們首先描述了在 Selenium 中處理視窗和標籤頁的基本方法,並透過示例演示瞭如何使用 Selenium Webdriver 處理視窗和標籤頁,以及 Selenium 中 close() 和 quit() 方法的區別。這使您深入瞭解 Selenium WebDriver - 視窗和標籤頁。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。