如何使用 WebDriver 檢視警示是否存在?
我們可以使用 Selenium webdriver 檢視警示是否存在。警示是藉助 Javascript 建立的。應當在同步中使用顯式等待概念驗證警示的存在。
讓我們考察一下下面的警示並檢查它是否存在於頁面上。有一個稱為alertIsPresent的條件,我們將使用它來檢視警示。它應等待特定時間來顯示警示,在這段時間後它將引發異常。
我們需要import org.openqa.selenium.support.ui.ExpectedConditions 和 import org.openqa.selenium.support.ui.WebDriverWait來合併預期條件和 WebDriverWait 類。

示例
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;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
public class VerifyAlert{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String u ="https://tutorialspoint.tw/selenium/selenium_automation_practice.htm"driver.get(u);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify and click submit
WebElement t = driver.findElement(By.name("submit"));
t.click();
// Explicit wait condition for alert
WebDriverWait w = new WebDriverWait(driver, 5);
//alertIsPresent() condition applied
if(w.until(ExpectedConditions.alertIsPresent())==null)
System.out.println("Alert not exists");
else
System.out.println("Alert exists");
driver.close();
}
}輸出

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP