如何使用 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.com/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