• Selenium Video Tutorials

Selenium WebDriver - 視窗和標籤頁



Selenium Webdriver 可用於處理視窗和標籤頁。Selenium Webdriver 處理視窗和標籤頁沒有區別。每個開啟的視窗都有一個該會話的唯一識別符號。

開啟新視窗後,驅動程式上下文仍然保留在父視窗上。為了在子視窗上執行某些任務,我們需要將驅動程式上下文從主視窗切換到子視窗。

在 Selenium 中處理視窗和標籤頁的基本方法

Selenium 中有多種方法可用於根據視窗和標籤頁自動化測試。要訪問子視窗,我們必須首先使用 switchTo().window("子視窗的視窗控制代碼 ID") 方法將驅動程式上下文切換到另一個視窗。處理視窗和標籤頁的方法如下所示:

  • driver.getWindowHandle() - 用於獲取焦點視窗的視窗控制代碼 ID。其返回型別為字串。
  • driver.getWindowHandles() - 用於獲取已開啟的所有視窗的視窗控制代碼 ID。其返回型別為字串集。

示例 1

讓我們以以下頁面為例,我們將點選新標籤頁按鈕。

Selenium Windows Tabs 1

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

Selenium Windows Tabs 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 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

讓我們再舉一個例子,如下面的圖片所示,我們將點選新視窗訊息按鈕。

Selenium Windows Tabs 3

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

Selenium Windows Tabs 4

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

Selenium Windows Tabs 5

程式碼實現

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)

讓我們再舉一個例子,我們將首先開啟一個視窗並啟動一個包含文字 - 複選框的應用程式。

Selenium Windows Tabs 6

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

Selenium Windows Tabs 7

程式碼實現

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)

讓我們再舉一個例子,我們將首先開啟一個視窗並啟動一個包含文字 - 複選框的應用程式。

Selenium Windows Tabs 8

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

Selenium Windows Tabs 9

在 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 相關的其他知識,以加深您的理解並擴充套件您的視野。

廣告