• Selenium Video Tutorials

Selenium WebDriver - 警報和彈出視窗



Selenium Webdriver 可用於處理警報和彈出視窗。網頁上的警報旨在顯示警告訊息、資訊或獲取使用者授權以繼續執行進一步的操作。

在 Selenium 中處理警報和彈出視窗的基本方法

Selenium 中有多種方法可用於根據警報和彈出視窗自動化測試。要訪問警報,我們必須首先使用 switchTo().alert() 方法將驅動程式上下文切換到警報。讓我們詳細討論 Alert 介面的一些方法 -

  • driver.switchTo().alert().accept() - 用於透過單擊警報上顯示的“確定”按鈕來接受警報。
  • driver.switchTo().alert().dismiss() - 用於透過單擊警報上顯示的“取消”按鈕來取消警報。
  • driver.switchTo().alert().getText() - 用於獲取警報上顯示的文字。
  • driver.switchTo().alert().sendKeys("要輸入的值") - 用於在警報上顯示的輸入文字上輸入一些文字。

請注意,在使用 Alert 介面時,我們需要在測試中新增匯入語句import org.openqa.selenium.Alert

示例 1 - 警報

讓我們討論一下網頁上的普通警報。單擊頁面上的“警報”按鈕後,將生成一個警報,文字為 - Hello world!,如下圖所示。

Selenium Alerts Popups 1

程式碼實現

package org.example;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class Alrts {
   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 get alert
      driver.get("https://tutorialspoint.tw/selenium/practice/alerts.php");
      
      // identify button for clicking to get alert
      WebElement c = driver.findElement(By.xpath("//button[text()='Alert']"));
      c.click();
      
      // switch driver context to alert
      Alert alrt = driver.switchTo().alert();
      
      // dismiss alert
      alrt.dismiss();
      
      //again get the alert
      c.click();
      
      // Get alert text
      String s = alrt.getText();
      System.out.println("Alert text is: " + s);
      
      // accept alert
      alrt.accept();
      
      // quitting the browser
      driver.quit();
   }
}

輸出

Alert text is: Hello world!

Process finished with exit code 0

在上面的示例中,我們捕獲了警報上的文字,並在控制檯中收到了訊息 - 警報文字為:Hello world!

最後,收到了訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。

示例 2 - 確認警報

讓我們討論一下網頁上的另一個警報,如下圖所示。單擊第二個單擊我按鈕後,我們將在網頁上獲得一個警報,文字為 - 按一個按鈕!

Selenium Alerts Popups 2

單擊警報上的“確定”按鈕後,我們將在網頁上獲得文字您按下了確定!

Selenium Alerts Popups 3

並且在單擊警報上的“取消”按鈕後,我們將在頁面上獲得文字您按下了取消!。

Selenium Alerts Popups 4

程式碼實現

package org.example;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class AlertJS {
   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 get alert
      driver.get("https://tutorialspoint.tw/selenium/practice/alerts.php");

      // identify button for clicking to get alert
      WebElement c =driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/div[3]/button"));
      c.click();

      // switch driver context to alert
      Alert alrt = driver.switchTo().alert();

      // Get alert text
      String s = alrt.getText();
      System.out.println("Alert text is: " + s);

      //accept alert
      alrt.accept();

      // get text after accepting alert
      WebElement text =driver.findElement(By.xpath("//*[@id='desk']"));
      System.out.println("Text appearing after alert accept: " + text.getText());

      // again get the alert
      c.click();

      // switch driver context to alert
      Alert alrt1 = driver.switchTo().alert();

      // now dismiss alert
      alrt1.dismiss();

      // get text after dismissing alert
      WebElement text1 =driver.findElement(By.xpath("//*[@id='desk']"));
      System.out.println("Text appearing after alert dismiss: " + text1.getText());

      // quitting the browser
      driver.quit();
   }
}

輸出

Alert text is: Press a button!
Text appearing after alert accept: You pressed OK!
Text appearing after alert dismiss: You pressed Cancel!

在上面的示例中,我們捕獲了警報上的文字,並在控制檯中收到了訊息 - 警報文字為:按一個按鈕!。然後在接受警報後,我們捕獲了頁面上顯示的文字,並在控制檯中顯示了訊息 - 警報接受後顯示的文字:您按下了確定!。此外,在取消警報後,我們捕獲了頁面上顯示的文字,並在控制檯中顯示了訊息 - 警報取消後顯示的文字:您按下了取消!

示例 3 - 提示警報

讓我們討論一下網頁上的另一個警報,如下圖所示。單擊第三個單擊我按鈕後,我們將在網頁上獲得一個警報,文字為 - 您的姓名是什麼?以及一個輸入框。

Selenium Alerts Popups 5

程式碼實現

package org.example;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class AlertsPromp {
   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 get alert
      driver.get("https://tutorialspoint.tw/selenium/practice/alerts.php");

      // identify button for clicking to get alert
      WebElement c =driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/div[4]/button"));
      c.click();

      // switch driver context to alert
      Alert alrt = driver.switchTo().alert();

      // Get alert text
      String s = alrt.getText();
      System.out.println("Alert text is: " + s);

      // enter text then accept alert
      alrt.sendKeys("Selenium");
      alrt.accept();

      // again get the alert
      c.click();

      // switch driver context to alert
      Alert alrt1 = driver.switchTo().alert();

      // again enter text then dismiss alert
      alrt1.sendKeys("Selenium");
      alrt1.dismiss();

      // quitting the browser
      driver.quit();
   }
}

輸出

Alert text is: What is your name?

在上面的示例中,我們捕獲了警報上的文字,並在控制檯中收到了訊息 - 警報文字為:您的姓名是什麼?

示例 4 - 帶有等待條件的警報

讓我們討論一下網頁上的另一個警報,如下圖所示。單擊第一個單擊我按鈕後,我們將獲得一個警報,文字為 - Hello just appeared,將在 5 秒後出現。

Selenium Alerts Popups 6

程式碼實現

package org.example;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;

public class AlertsPromp {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // Opening the webpage where we will get alert
      driver.get("https://tutorialspoint.tw/selenium/practice/alerts.php");

      // identify button for clicking to get alert
      WebElement c = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/div[2]/button"));
      c.click();

      // explicit wait to expected condition for presence alert
      WebDriverWait wt = new WebDriverWait(driver, Duration.ofSeconds(6));
      wt.until(ExpectedConditions.alertIsPresent());

      // switch driver context to alert
      Alert alrt = driver.switchTo().alert();

      // Get alert text
      String s = alrt.getText();
      System.out.println("Text is: " + s);

      // accept alert
      alrt.accept();

      // quitting the browser
      driver.quit();
   }
}

輸出

Text is: Hello just appeared

結論

這結束了我們關於 Selenium WebDriver 警報和彈出視窗教程的全面介紹。我們從描述在 Selenium 中處理警報和彈出視窗的基本方法開始,並透過示例說明如何在 Selenium Webdriver 中處理不同型別的警報和彈出視窗。這使您深入瞭解 Selenium WebDriver 警報和彈出視窗。明智的做法是不斷練習您學到的知識並探索與 Selenium 相關的其他內容,以加深您的理解並擴充套件您的視野。

廣告

© . All rights reserved.