トップページ > Appium API(逆引き) > 【Appium Python】XPathから要素を取得する

【Appium Python】XPathから要素を取得する

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

find_element(AppiumBy.XPATH, "xxx")の"xxx"にXPathを指定することで要素を取得することが出来ます。


Appium Inspectorで対象のアプリの「App Source」と「Selected Element」の内容を確認しながら要素を調査していきします。

取得したい要素にid属性値が存在している場合は、find_element(AppiumBy.ID, "xxx")を用いることで要素を特定して取得することが可能です。
ID属性が存在しない場合やXPathを用いる場合は、他の属性値などを指定して要素がユニークに特定できるように指定する必要があります。


下記のサンプルでは、「どちらでもない」ラジオボタンをクリック後、「今川講師」のリストビューをクリックしています。

※Appiumでのid属性値などの要素調査方法についてはこちらを参考にしてください。

説明

◆メソッド
  ○XPathから要素を取得する
       ・driver.find_element(AppiumBy.XPATH, "xxx")
◆備考
  ・ー
◆その他「Appium API」
  ・要素にキー入力した値を取得/クリアする
  ・属性値を取得し要素が選択されているか判定する

サンプル

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)

#「どちらでもない」ラジオボタンをクリック
test_session.find_element(AppiumBy.XPATH, "//android.widget.RadioButton[3]").click()
#3秒間待機
time.sleep(3)
#「3.今川講師」リストビューをクリック
test_session.find_element(AppiumBy.XPATH, "//android.widget.TextView[3][@text='3.今川講師']").click()
#3秒間待機
time.sleep(3)
※Appium v2.0.0-beta.55で動作確認をしています

要素の取得

xpath1


xpath2

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

Appium PythonAPI TOP10
過去1週間(4/17~4/23)

    カテゴリー

    環境構築

    APIリファレンス

    その他

    ページ上部へ戻る
    トップページ > Appium API(逆引き) > 【Appium Python】XPathから要素を取得する
    Copyright © 2016- Seleniumクイックリファレンス All Rights Reserved