- Selenium 教程
- Selenium - 首頁
- Selenium - 概覽
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境搭建
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- 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 - 動態網頁表格
- 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 中處理多個視窗的基本方法
Selenium 中有各種方法可用於自動化處理多個視窗的測試。要在子視窗上工作,需要將驅動程式上下文從父視窗切換到子視窗。
示例 1
在下面的頁面中,單擊新標籤頁。
單擊新標籤頁後,我們將切換到另一個包含文字新標籤頁的標籤頁。
程式碼實現
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class TabsHandling {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Opening the webpage with new tab
driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php");
// click link then navigate to next tab
WebElement b = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[1]"));
b.click();
// Get original window handle id
String oW = driver.getWindowHandle();
// get every windows handle ids
Set<String> windows = driver.getWindowHandles();
// loop through all window handles
for (String w : windows) {
if(!oW.equalsIgnoreCase(w)) {
// switch to the child tab
driver.switchTo().window(w);
// get element in new tab
WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1"));
System.out.println("Text in new tab: " + e.getText());
break;
}
}
// quit 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.edge.EdgeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class WindowsOpen {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 20 secs
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Opening the webpage to open a new window
driver.get("https://tutorialspoint.tw/selenium/practice/browser-windows.php");
// click link to next window
WebElement b = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/button[3]"));
b.click();
// Obtain original window handle id
String oW = driver.getWindowHandle();
// obtain all opened windows handle ids
Set<String> windows = driver.getWindowHandles();
// Loop through all window handles
for (String w : windows) {
if(!oW.equalsIgnoreCase(w)) {
// switch to child window
driver.switchTo().window(w);
// get element in new window
WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/h1"));
System.out.println("Text in new window: " + e.getText());
driver.close();
break;
}
}
// switch to parent window
driver.switchTo().window(oW);
// get 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: " + e1.getText());
// quit browser session
driver.quit();
}
}
輸出
Text in new window: New Window Message Text in parent window: Browser Windows
在這裡,我們捕獲了新視窗中的文字,並在控制檯中獲得了訊息 - 新視窗中的文字:新視窗訊息。然後,我們結束子視窗並返回到父視窗。最後,我們在控制檯中獲取了父視窗中的文字 - 父視窗中的文字:瀏覽器視窗。
因此,close() 和 quit() 方法之間存在細微差別。close() 方法僅關閉活動瀏覽器視窗,而 quit() 方法同時終止所有開啟的瀏覽器視窗。
示例 3
從版本 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.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;
public class NewWindowOpen {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// Open a webpage in first window
driver.get("https://tutorialspoint.tw/selenium/practice/check-box.php");
// get text in first window
WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text: " + e.getText());
// Initiate the another Webdriver
WebDriver newDriver = driver.switchTo().newWindow(WindowType.WINDOW);
// Opening another webpage in second window
driver.get("https://tutorialspoint.tw/selenium/practice/radio-button.php");
// get text in second window
WebElement e1 = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/h1"));
System.out.println("Text in new window: " + e1.getText());
// quit the browser session
driver.quit();
}
}
輸出
Text: Check Box Text in new window: Radio Button
在這裡,我們捕獲了第一個視窗中的文字,並在控制檯中獲得了訊息 - 文字:複選框。然後開啟另一個新視窗,並在其中啟動一個應用程式。最後,我們在新視窗中獲取了文字,並在控制檯中獲得了一條訊息 - 新視窗中的文字:單選按鈕。
示例 4
從版本 4 開始,我們可以使用以下方法開啟一個新標籤頁 -
driver.switchTo().newWindow(WindowType.TAB)
在瀏覽器中開啟一個應用程式,並獲取文字 - 如下圖所示的複選框 -
然後開啟另一個新標籤頁並啟動一個包含文字 - 單選按鈕的不同應用程式。
程式碼實現
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.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;
public class NewTabsOpen {
public static void main(String[] args) throws InterruptedException {
// Initiate the Webdriver
WebDriver driver = new EdgeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
// Open a webpage in first window
driver.get("https://tutorialspoint.tw/selenium/practice/check-box.php");
// get text in first window
WebElement e = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/h1"));
System.out.println("Text: " + e.getText());
// Initiate the another Webdriver
WebDriver newDriver = driver.switchTo().newWindow(WindowType.TAB);
// Open a webpage in new tab
driver.get("https://tutorialspoint.tw/selenium/practice/radio-button.php");
// obtain 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: " + e1.getText());
// quit the browser session
driver.quit();
}
}
輸出
Text: Check Box Text in other tab: Radio Button
在這裡,我們捕獲了第一個視窗中的文字,並在控制檯中獲得了訊息 - 文字:複選框。然後開啟另一個新標籤頁,並在其中啟動一個應用程式。最後,我們在新標籤頁中獲取了文字,並在控制檯中獲得了一條訊息 - 其他標籤頁中的文字:單選按鈕。
結論
這總結了我們關於 Selenium Webdriver 多視窗測試教程的全面介紹。我們從描述在 Selenium 中處理多個視窗的基本方法開始,並逐步介紹瞭如何使用 Selenium Webdriver 處理多個視窗的示例。這使您深入瞭解 Selenium Webdriver - 多視窗測試。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。
