トップページ > Appium API(逆引き) > 【Appium Python】属性値を取得し要素が選択されているか判定する

【Appium Python】属性値を取得し要素が選択されているか判定する

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

element.get_attribute("xxx")の"xxx"に任意の属性名を指定することで、その属性名の属性値を取得することが出来ます。

下記のサンプルでは、ラジオボタンの要素(どちらでもない)を取得しています。続いて、その要素が持っている属性の「checked」をget_attribute()の引数に指定することで、「checked」の値を取得することが出来、ラジオボタンがチェックされているかを判定することが出来ます。

説明

◆メソッド
  ○要素の属性値を取得する
       ・value = element.get_attribute("xxx")
◆備考 ー

◆その他「Appium API」
  ・要素のXY座標とheightとwidthを取得する
  ・要素が見つかるまでの待ち時間を設定する

サンプル

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
import time

#接続したいアプリ情報を変数に格納
desired_caps = {}
desired_caps["appium:app"] = "C:\\appium_test\\app-debug.apk"
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "12.0"
desired_caps["appium:deviceName"] = "PixelXLAPI31"
desired_caps["appium:automationName"] = "UiAutomator2"
#セッションの作成
test_session = webdriver.Remote(
    command_executor='http://localhost:4723/wd/hub',
    desired_capabilities= desired_caps)

#ラジオボタンの要素(どちらでもない)を取得
element = test_session.find_element(AppiumBy.ID, "ra_neither")
#取得した要素がチェックされているか確認
check_radio = element.get_attribute("checked")
print(check_radio) # false
time.sleep(3)
#ラジオボタンの要素(どちらでもない)をクリック
element.click()
#取得した要素がチェックされているか確認
check_radio = element.get_attribute("checked")
print(check_radio) # true
time.sleep(3)
※Appium v2.0.0-beta.55で動作確認をしています

属性の確認

att01

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: (404) <!DOCTYPE html> <html lang=en> <meta charset=utf-8> <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> <title>Error 404 (Not Found)!!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/googlelogo_c in /home/users/1/monda-muki/web/seleniumqref.com/google-api-php-client-1-master/src/Google/Http/REST.php on line 110