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

【Java】implicitlyWait・・・指定したドライバの要素が見つかるまでの待ち時間を設定する

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

「implicitlyWait」を実行することで、指定したドライバの要素が見つかるまでの待ち時間を設定出来ます。

下記サンプルでは、GoogleTopページに遷移する前に、指定したドライバに対して「implicitlyWait」を実行しています。「implicitlyWait」の引数に数値(秒数)を指定することで、指定した待ち時間の間、要素が見つかるまで(ロードされるまで)待機します。また、「implicitlyWait」で指定されたdriverの要素全てに対して、見つかるまでの待ち時間を一律に指定していきます。 (下記サンプルに話を戻し)続いて「driver.findElement(By.id("abc"))」で要素取得を試みていますが、指定したidである"abc"はGoogleTopページに実在しないidである為、要素を見つけることができません。しかし、画面遷移直後に要素がロードされていない時に待機するのと同じように、HTML上に実在しないidで要素取得を試みた場合でも、「implicitlyWait」で指定した待ち時間分待機します。

説明

◆メソッド
  ・WebDriver.Timeouts implicitlyWait(long time, java.util.concurrent.TimeUnit unit)
◆使用形態
  ・driver.manage().timeouts().implicitlyWait(time,TimeUnit)
◆備考
  ・driver.manage().timeouts()の形でタイムアウトの振る舞いを管理する
  ・設定した待ち時間を越えても要素が見つからない場合は、Exceptionが発生
◆関連項目
  ・ページがロードされまでの待ち時間を設定する
  ・jjavascript実行が終了するまでの待ち時間を設定する

サンプル

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

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();
    //要素が見つからない場合最大で10秒間待つよう指定
    Duration waitTime = Duration.ofSeconds(10);
    driver.manage().timeouts().implicitlyWait(waitTime);
    //指定したURLに遷移する
    driver.get("https://www.google.co.jp");
    //存在しないIDを指定。10秒待った後にExceptionが発生するか確認。
    WebElement element = driver.findElement(By.id("abc"));
  }
}
※selenium version 4.1.4で動作確認をしています(ブラウザのUIや属性値などが変更された場合、実行結果が異なる可能性があります)

実行結果(10秒後にExceptionが発生)

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"abc"}
・
・
・

動画デモ

※字幕を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