1. 程式人生 > >Selenium 判斷Alert是否存在

Selenium 判斷Alert是否存在

//獲取驅動
public static WebDriver getDriver() {
    //System.setProperty("webdriver.chrome.driver","other/../../../chromedriver.exe");
    WebDriver drivers = new ChromeDriver();
    drivers.get(OpenBrowers);
    drivers.navigate().refresh();
    return drivers;
}

public boolean isAlertPersent(){
    try {
        getDriver().switchTo().alert();
        return true;
    }catch (NoAlertPresentException e){return false;}
}

public String getAlert(){
    Alert alert = getDriver().switchTo().alert();
    String text = alert.getText();
    alert.accept();
    return text;
}

public void closeAllALert(){
    while (isAlertPersent()){
        getDriver().switchTo().alert().accept();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}