トップページ > Selenium API(逆引き) > 【Ruby】displayed?

【Ruby】displayed?・・・要素が表示されているか判定する

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

要素に対して「displayed?」を実行することで要素がWebページ上に表示されているか調べることが出来ます。

「displayed?」はstyle属性で非表示になっている場合にFalseが返されます。

下記のサンプルでは、ユーザ名テキストボックスとパスワードテキストボックスの要素を取得し、「displayed?」を実行しています。パスワードテキストボックスは、javascriptで非表示の処理を実施している為、Falseが返されるという結果になっています。

説明

◆メソッド
  ・displayed?
◆使用形態
  ・element.displayed?
◆備考
  ・要素が表示されている場合はTrueを返し、非表示の場合はFalseを返す
◆関連項目
  ・要素が有効になっているか判定する
  ・要素が選択されているか判定する

サンプル

require 'selenium-webdriver'
 
# Firefoxを起動
driver = Selenium::WebDriver.for(:firefox)
#指定したURLに遷移する<図1>
driver.get('file://D:/AutoTest/isDisplayed.html')
#ユーザ名テキストボックスの要素を取得
elemUser = driver.find_element(:id, 'user01')
#パスワードテキストボックスの要素を取得
elemPass = driver.find_element(:id, 'pass01')
#ユーザ名テキストボックスの表示状態を判定
puts(elemUser.displayed?)
#パスワードテキストボックスの表示状態を判定
puts(elemPass.displayed?)
※selenium version 4.1.0で動作確認をしています

実行結果

True
False
isDisplayed2

isDisplayed.html<図1>

<!DOCTYPE html>
<html lang="ja">
    <head>
        <title>isDisplayed</title>
    </head>
    <body>
        <p><label for='user01'>ユーザ名:</label><input type="text" id="user01" value="user"></p>
        <p><label for='pass01'>パスワード:</label><input type="text" id="pass01"></p>
        <script>
            function hideBtn(){
                var txtElm = document.getElementById("pass01");
                txtElm.style.visibility = "hidden";
            }
            hideBtn();
        </script>
    </body>
</html>

動画デモ

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


Warning: Invalid argument supplied for foreach() in /home/users/1/monda-muki/web/seleniumqref.com/api/ruby/element_judge/Ruby_displayed.html on line 186

RubyAPIアクセス TOP10
過去1週間(3/12~3/18)

    カテゴリー

    環境構築

    APIリファレンス

    その他

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