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

【Java】textToBePresentInElement・・・指定したテキストが表示されるまで待機する

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

「textToBePresentInElement」を実行することで、指定したテキストが表示されるまで待機することが出来ます。

下記サンプルではまず初めに、「WebDriverWait」コンストラクタの引数に対象driverと待ち時間(秒数)を指定して、インスタンスを作成しています。

次に作成されたインスタンスに対して「until」メソッドを実行しています。「until」メソッドは、引数に指定された条件が真になるまで待機するメソッドです。引数に指定するメソッドの「textToBePresentInElement」は、「ExpectedConditions.textToBePresentInElement」の形で用いることで、指定したテキストが表示されているかどうかを確認することができます。また、「textToBePresentInElement」は引数を設定する必要があります。第1引数で取得した要素を指定し、第2引数に期待値とするテキストを指定します。つまり全て総合すると指定した要素のテキストが表示されるまで待つということになります。もしテキストが「WebDriverWait」コンストラクタで指定した待ち時間を越えても、期待値の状態にならない場合はExceptionが発生します。今回のケースでは、HTML内のjavascriptで5秒後に、期待値と一致するテキストが表示されるように設定しています。待ち時間を10秒に設定しているのでExceptionは発生しません。

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

説明

◆メソッド
  ・public static ExpectedCondition<java.lang.Boolean>   
                            textToBePresentInElement(WebElement element,java.lang.String text)
◆使用形態
  ・ExpectedConditions.textToBePresentInElement(element,text)
◆備考
  ・指定した要素内の指定したテキストが可視状態になるまで待機する
  ・最大待機時間を越えてもテキストが表示されない場合はExceptionが発生する
◆関連項目
  ・指定したテキストが非表示にまるまで待機する
  ・指定した要素がDOM上に現れるまで待機する

サンプル

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;
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();
	try{
	  //指定したURLに遷移する<図1>
	  driver.get("file://D:/AutoTest/textToBePresentInElement.html");
	  //指定したdriverに対して最大で10秒間待つように設定する
      Duration waitTime = Duration.ofSeconds(10);
      WebDriverWait wait = new WebDriverWait(driver, waitTime);
	  //テキスト表示される要素を取得する
	  WebElement element = driver.findElement(By.id("preText"));
	  //表示確認したいテキストを指定する
	  String text = "指定したテキストが表示されるまで待機する";
	  //要素に指定したテキストが表示されるまで待機する
	  boolean isTxt = wait.until(ExpectedConditions.
                            textToBePresentInElement(element, text));
	  //指定したテキストが表示されているか確認
	  System.out.println(isTxt);
	}catch(Exception e){
	  e.printStackTrace();
	}
  }
}
※selenium version 4.1.4で動作確認をしています

実行結果

true
textToBePresentInElement

textToBePresentInElement.html<図1>

<!DOCTYPE html>
<html lang="ja">
    <head>
        <title>textToBePresentInElement</title>
    </head>
    <body>
        <p id="preText" style="visibility:hidden">指定したテキストが表示されるまで待機する</p>
        <script>
            function presentTxt(){
                var txtElm = document.getElementById("preText");
                txtElm.style.visibility = "visible";
            }
            setTimeout("presentTxt()", 5000);
        </script>
    </body>
</html>

動画デモ

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


Warning: Invalid argument supplied for foreach() in /home/users/1/monda-muki/web/seleniumqref.com/api/java/conditions/Java_textToBePresentInElement.html on line 211

JavaAPIアクセス TOP10
過去1週間(4/19~4/25)

    カテゴリー

    環境構築

    APIリファレンス

    その他

    ページ上部へ戻る
    トップページ > Selenium API(逆引き) > 【Java】textToBePresentInElement
    Copyright © 2016- Seleniumクイックリファレンス All Rights Reserved