- 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 - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver 與 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 中處理警報和彈出視窗的基本方法
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!,如下圖所示。
程式碼實現
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 - 確認警報
讓我們討論一下網頁上的另一個警報,如下圖所示。單擊第二個單擊我按鈕後,我們將在網頁上獲得一個警報,文字為 - 按一個按鈕!。
單擊警報上的“確定”按鈕後,我們將在網頁上獲得文字您按下了確定!。
並且在單擊警報上的“取消”按鈕後,我們將在頁面上獲得文字您按下了取消!。
程式碼實現
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 - 提示警報
讓我們討論一下網頁上的另一個警報,如下圖所示。單擊第三個單擊我按鈕後,我們將在網頁上獲得一個警報,文字為 - 您的姓名是什麼?以及一個輸入框。
程式碼實現
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 秒後出現。
程式碼實現
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 相關的其他內容,以加深您的理解並擴充套件您的視野。
