appium获取元素节点的方法_贝程学院:Appium之Appium元素定位方法(三)

ead291e93bd25f572db4cc78aa201167.png

6.方法:find_element_by_android_uiautomator(uia_string)

  • 作用:通过底层Android UiAutomator自动化测试框架定位控件。
  • 参数:参数是UiAutomator的 UiSelector对象来定位。

A.text属性的方法

实例:实现计算器加法用例代码

from appium import webdriver
import time

desired_caps = {
    'platformName': 'Android',
    'deviceName': 'Android Emulator',
    'platformVersion': '6.0',
    'appPackage': 'com.android.calculator2',
    'appActivity': '.Calculator'
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(4)

driver.find_element_by_android_uiautomator('new UiSelector().text("1")').click()
driver.find_element_by_android_uiautomator('new UiSelector().text("+")').click()
driver.find_element_by_android_uiautomator('new UiSelector().text("1")').click()
driver.find_element_by_android_uiautomator('new UiSelector().text("=")').click()

B.class属性的方法

实例:实现计算器加法用例代码

from appium import webdriver
import time

desired_caps = {
    'platformName': 'Android',
    'deviceName': 'Android Emulator',
    'platformVersion': '6.0',
    'appPackage': 'com.android.calculator2',
    'appActivity': '.Calculator'
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(4)

driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.Button").text("1")').click()
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.Button").text("+")').click()
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.Button").text("1")').click()
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.Button").text("=")').click()

技术解释:获取控件的方式多种多样,都是通过UiSelector对象来去查找。

7.方法:driver.Page_Source

AppiumDriver的getPageSource函数,可以把当前页面的控件以XML的方式打印出来。在定位不到某个控件属性的时候,可以在返回值中查找。

实例:打印计算器应用当前页面控件

from appium import webdriver
import time

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['appPackage'] = 'com.android.calculator2'
desired_caps['appActivity'] = '.Calculator'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

print(driver.page_source)

输出:

<?xml version="1.0" encoding="UTF-8"?><hierarchy rotation="0">..省略….</android.widget.LinearLayout></android.widget.FrameLayout></hierarchy>

8.WebView定位: find_element_by_Link_Text ()

针对WebView容器下面的控件定位的。

9.WebView定位: find_element_bypartial_link_text

针对WebView容器下面的控件定位的。

10.WebView定位:find_Element_By_Tag_Name

这个方法Appium1.0以后已经过时并被find_Element_By_Class_Name取代。

11.WebView定位: find_element_by_css_selector()

作用:针对WebView容器下面的控件定位


版权声明:本文为weixin_35782323原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。