トップページ > Selenium API(逆引き) > 【Java】alertIsPresent

【Java】alertIsPresent・・・Alertが表示されるまで待機する

【PR】業界トップクラスの「高単価報酬」「低マージン」を実現!レバテックフリーランス

「alertIsPresent」を実行することで、Alertが表示されるまで待機することが出来ます。

下記サンプルでは、指定したHTMLにページ遷移することで、HTML内のjavascriptを実行し、Alertを表示しています。そして「WebDriverWait」コンストラクタの引数に、対象driverと待ち時間(秒数)を指定して、インスタンスを作成しています。

次に作成されたインスタンスに対して「until」メソッドを実行しています。「until」メソッドは、引数に指定された条件が真になるまで待機するメソッドです。引数に指定するメソッドの「alertIsPresent」は、「ExpectedConditions.alertIsPresent」の形で用いることでアラートの表示を意味します。つまり、アラートが表示されるまで待つということになります。もしアラートが「WebDriverWait」コンストラクタで指定した待ち時間を越えても表示されない場合、Exceptionが発生します。

※条件を指定して待機についてはこちら

説明

◆メソッド
  ・public static ExpectedCondition<Alert> alertIsPresent()
◆使用形態
  ・ExpectedConditions.alertIsPresent()
◆備考
  ・最大待機時間を越えてもAlertが表示されない場合はExceptionが発生する
◆関連項目
  ・要素がチェックONまたはチェックOFFになるまで待機する
  ・要素がクリック出来る状態になるまで待機する

サンプル

import java.time.Duration;

import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WebTest{
  public static void main(String[] args){
    //Chrome制御のためChromeDriverのパスを指定
	System.setProperty("webdriver.chrome.driver",
		                            "D:\\AutoTest\\chromedriver.exe");
	//Chromeを起動する
	WebDriver driver = new ChromeDriver();
    //指定したURLに遷移する<図1>
    driver.get("file://D:/AutoTest/alertIsPresent.html");
    //指定したdriverに対して最大で10秒間待つように設定する
    Duration waitTime = Duration.ofSeconds(10);
    WebDriverWait wait = new WebDriverWait(driver, waitTime);
    //Alertが表示されるまで待機するよう設定
    Alert alert = wait.until(ExpectedConditions.alertIsPresent());
    //3秒間待機する(Alertが表示されていることを確認するため)
    try{
          Thread.sleep(3000);
    }catch(InterruptedException e){
          e.printStackTrace();
    }
    //AlertのOKボタンを押下する
    alert.accept();
  }
}
※selenium version 4.1.4で動作確認をしています

実行結果(OKボタンが押下される直前)

alertIsPresent

Java_alertIsPresent.html<図1>

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="UTF-8">
        <title>alertIsPresent</title>
    </head>
    <body>
        <script>
            alert("Alert appeared!");
        </script>
    </body>
</html>

動画デモ

※字幕をONにすると解説のテロップが表示されます※

>

Fatal error: Uncaught Google_Service_Exception: Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A138132118&start-date=7daysAgo&end-date=yesterday&metrics=ga%3Apageviews&dimensions=ga%3ApageTitle%2C+ga%3ApagePath&sort=-ga%3Apageviews&max-results=1000: (502) <!DOCTYPE html> <html lang=en> <meta charset=utf-8> <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> <title>Error 502 (Server Error)!!1</title> <style> *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelog in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php on line 110