<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Selenium WebDriver 中的警報&彈出窗口處理 > 原文: [https://www.guru99.com/alert-popup-handling-selenium.html](https://www.guru99.com/alert-popup-handling-selenium.html) 在本教程中,我們將學習在 Web 應用程序[測試](/software-testing.html)中發現的不同類型的警報,以及如何在 Selenium WebDriver 中處理警報。 我們還將看到我們如何根據警報類型來接受和拒絕警報。 在本教程中,您將學習- * [什么是警報?](#1) * [如何在 Selenium WebDriver](#2) 中處理警報 * [如何使用 Webdriver 處理[Selenium Popup]窗口](#3) ## 什么是警報? 警報是一個小消息框,它在屏幕上顯示通知,以向用戶提供某種信息或請求允許執行某種操作。 它也可以用于警告目的。 以下是幾種警報類型: **1)簡單警報** 這個簡單的警報在屏幕上顯示一些信息或警告。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/bb/bd/bbbd67f31b547a1275f249899d91bbf5_873x268.png "Alert & Popup handling in Selenium") **2)提示警報。** 該提示警報要求用戶提供一些輸入,Selenium Webdriver 可以使用 sendkeys(“ input…。”)輸入文本。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/83/c9/83c9f7e83997c572a7b56847f93b97ac_757x427.png "Alert & Popup handling in Selenium") **3)確認警報。** 該確認警報要求獲得執行某種類型的操作的權限。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/10/aa/10aa624e5386cd1ac9939f740548a74e_822x379.png "Alert & Popup handling in Selenium") ## 如何處理 Selenium WebDriver 中的警報 Alert 界面提供了以下幾種在 Selenium Webdriver 中廣泛使用的方法。 1)void dismiss() **//單擊警報的“取消”按鈕。** ``` driver.switchTo().alert().dismiss(); ``` 2)void accept() **//單擊警報的“確定”按鈕。** ``` driver.switchTo().alert().accept(); ``` 3)String getText **()//捕獲警報消息。** ``` driver.switchTo().alert().getText(); ``` 4)void sendKeys(String stringToSend) **//向警報框發送一些數據。** ``` driver.switchTo().alert().sendKeys("Text"); ``` 您可以看到顯示了許多 Alert 方法,如 Eclipse 建議的以下屏幕所示。 我們可以使用 Selenium 的 **.switchTo()**方法輕松地從主窗口切換到警報。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/62/28/622859b5482f741154eeda0db7031c7b_952x467.png "Alert & Popup handling in Selenium") **現在,我們將自動化給定的以下場景。** 在這種情況下,我們將使用 Guru99 演示站點來說明 Selenium Alert 的處理。 **步驟 1)**啟動 Web 瀏覽器并打開站點“ [http://demo.guru99.com/test/delete_customer.php](http://demo.guru99.com/test/delete_customer.php) ” **步驟 2)**輸入任何客戶 ID。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/c6/fd/c6fd70184f31bd9a16ab845bccbf83cc_536x124.png "Alert & Popup handling in Selenium") **步驟 3)**輸入客戶 ID 后,單擊“提交”按鈕。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/32/d0/32d037c7456fefbae53a0cfa0b7a2c9f_331x238.png "Alert & Popup handling in Selenium") **步驟 4)**拒絕/接受警報。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/28/2b/282b936980b78c35de29c658f7ae7de6_687x404.png "Alert & Popup handling in Selenium") **使用上述場景**處理 Selenium Webdriver 中的警報 ``` import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.NoAlertPresentException; import org.openqa.selenium.Alert; public class AlertDemo { public static void main(String[] args) throws NoAlertPresentException,InterruptedException { System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // Alert Message handling driver.get("http://demo.guru99.com/test/delete_customer.php"); driver.findElement(By.name("cusid")).sendKeys("53920"); driver.findElement(By.name("submit")).submit(); // Switching to Alert Alert alert = driver.switchTo().alert(); // Capturing alert message. String alertMessage= driver.switchTo().alert().getText(); // Displaying alert message System.out.println(alertMessage); Thread.sleep(5000); // Accepting alert alert.accept(); } } ``` **輸出:** 當您執行上述代碼時,它將啟動該站點。 嘗試通過處理屏幕上顯示的確認警報來刪除客戶 ID,從而從應用程序中刪除客戶 ID。 ## 如何使用 Webdriver 處理 Selenium 彈出窗口 在自動化中,當我們在任何 Web 應用程序中都有多個窗口時,活動可能需要將多個窗口之間的控制權從一個切換到另一個,以完成操作。 操作完成后,它必須返回到主窗口,即父窗口。 我們將在本文中通過示例進一步說明這一點。 在 Selenium Web 驅動程序中,有一些方法可以處理多個窗口。 **Driver.getWindowHandles();** 要通過 Web 驅動程序處理所有打開的窗口,可以使用“ Driver.getWindowHandles()”,然后可以在 Web 應用程序中將窗口從一個窗口切換到另一個窗口。 它的返回類型是 Iterator < String >。 **Driver.getWindowHandle();** 當站點打開時,我們需要通過 **driver.getWindowHandle()**處理主窗口。 這將處理在此驅動程序實例中唯一標識它的當前窗口。 它的返回類型是 String。 要在 Selenium WebDriver 中處理多個窗口,請執行以下步驟。 現在,我們將自動執行以下給定方案,以了解如何使用 Selenium Webdriver 處理多個窗口。 在這種情況下,我們將使用“ Guru99”演示站點來說明窗口處理。 **步驟 1)**啟動站點。 啟動瀏覽器并打開站點“ **http://demo.guru99.com/popup.php** ” ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/9d/ae/9dae4489a87a907d1944ecf13dd59d9e_537x81.png "Alert & Popup handling in Selenium") **步驟 2)**單擊鏈接“單擊此處”。 當用戶單擊“單擊此處”鏈接時,將打開新的子窗口。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/d4/e9/d4e99dcfeea8c6c8976734603474a0c9_907x231.png "Alert & Popup handling in Selenium") **步驟 3)**新建子窗口打開。 將打開一個新窗口,要求用戶輸入電子郵件 ID 并提交頁面。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/f6/e7/f6e7e3bcbb7535b5f307cddf27e47573_962x338.png "Alert & Popup handling in Selenium") **步驟 4)**輸入您的電子郵件 ID 并提交。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/e8/d6/e8d6bcef0805e928228276e51534b6a7_592x241.png "Alert & Popup handling in Selenium") **步驟 5)**在提交頁面時顯示訪問憑據。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/6b/9f/6b9f0fff7c03b0853556fcad17929f99_575x290.png "Alert & Popup handling in Selenium") 執行代碼時,您會看到子窗口在新選項卡中打開。 1. 關閉在其上顯示憑據的子窗口。 [![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/4c/86/4c860b12d73e0d434025ded3403a3e56_486x261.png "Alert & Popup handling in Selenium") ](/images/3-2016/032216_1314_AlertPopuph16.png) 2. 切換到父窗口。 [![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/73/4b/734b06324acea8ea1bc921f13f775ffb_977x236.png "Alert & Popup handling in Selenium") ](/images/3-2016/032216_1314_AlertPopuph17.png) **使用上述方案在 Selenium Webdriver 中處理多個窗口。** ``` import java.util.Iterator; import java.util.Set; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class WindowHandle_Demo { public static void main(String[] args) throws InterruptedException { WebDriver driver=new FirefoxDriver(); //Launching the site. driver.get("http://demo.guru99.com/popup.php"); driver.manage().window().maximize(); driver.findElement(By.xpath("//*[contains(@href,'popup.php')]")).click(); String MainWindow=driver.getWindowHandle(); // To handle all new opened window. Set<String> s1=driver.getWindowHandles(); Iterator<String> i1=s1.iterator(); while(i1.hasNext()) { String ChildWindow=i1.next(); if(!MainWindow.equalsIgnoreCase(ChildWindow)) { // Switching to Child window driver.switchTo().window(ChildWindow); driver.findElement(By.name("emailid")) .sendKeys("This email address is being protected from spambots. You need JavaScript enabled to view it. "); driver.findElement(By.name("btnLogin")).click(); // Closing the Child Window. driver.close(); } } // Switching to Parent window i.e Main Window. driver.switchTo().window(MainWindow); } } ``` **輸出:** 當您執行上述代碼時,它將啟動該站點,并單擊鏈接“單擊此處”,從而在新選項卡中打開一個子窗口。 您可以關閉子窗口,并在操作完成后切換到父窗口。 因此,在應用程序中處理多個窗口。 ![Alert & Popup Handling in Selenium WebDriver](https://img.kancloud.cn/0d/ce/0dce000aa1cf3fa1e6919422c4f031e5_715x418.png "Alert & Popup handling in Selenium") **結論:** * 我們定義了警報的類型,并通過屏幕截圖顯示了它們。 * 演示了使用特定場景使用 Selenium WebDriver 處理警報。 * 使用 Selenium WebDriver 在特定情況下處理了多個窗口。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看