トップページ > Appium API(逆引き) > 【Appium Python】要素にキー入力した値を取得/クリアする

【Appium Python】要素にキー入力した値を取得/クリアする

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

テキストボックス、テキストエリアなどの値を入力できる要素に対してキー入力するには、element.send_keys("xxx")の形でxxxに入力したい文字列を指定します。

一方、要素に入力されている文字列取得したい場合、element.textで入力されている値を取得し変数に格納後利用していきます。

また、要素に入力されている文字列をクリアしたい場合、element.celar()で要素を未入力の形にしていきます。

下記のサンプルでは、まずアプリの上部にあるテキストボックスの要素を取得し、キー入力で文字列を入力しています。続いて、入力された文字列を取得後コンソール出力して取得した値を確認しています。そして最後に要素に入力された値をクリアしています。

説明

◆メソッド
  ○要素に対してキー入力する
       ・element.send_keys()

  ○要素に入力された値を取得する
       ・txt = element.text

  ○要素に入力された値をクリアする
       ・element.celar()
◆備考
  ・ー
◆その他「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)

#テキストボックスの要素をID属性値から取得する
element = test_session.find_element(AppiumBy.ID, "etName")
#テキストボックスをクリックする
element.click()
#文字列を入力する
element.send_keys("key input !!")
#3秒間待機
time.sleep(3)
#入力された文字列を取得
txt = element.text
print(txt) #key input !!
#入力された文字列をクリア
element.clear()
#3秒間待機
time.sleep(3)
※Appium v2.0.0-beta.55で動作確認をしています

実行結果

key01
yajirushi
clear01

Warning: Invalid argument supplied for foreach() in /home/users/1/monda-muki/web/seleniumqref.com/api/appium_python/element/key_get_clear_value_in_the_element-AppiumPython.html on line 185

Appium PythonAPI TOP10
過去1週間(4/12~4/18)

    カテゴリー

    環境構築

    APIリファレンス

    その他

    ページ上部へ戻る
    トップページ > Appium API(逆引き) > 【Appium Python】要素にキー入力した値を取得/クリアする
    Copyright © 2016- Seleniumクイックリファレンス All Rights Reserved