Appium (six): Element Positioning

1. Positioning elements

For automated testing, core skills is to locate the object. Whether buttons or input boxes on web pages, or a button or input box on the mobile app, we want to be or click input operation, the prerequisite is to find the object.

webdriver provides a method for positioning of eight elements:

  • id
  • name
  • class name
  • tag name
  • link text
  • partial link text
  • xpath
  • css selector

Python language in the corresponding positioning method are as follows:

find_element_by_id()

find_element_by_name()

find_element_by_class_name()

find_element_by_tag_name()

find_element_by_link_text()

find_element_by_partial_link_text()

find_element_by_xpath()

find_element_by_css_selector()

The method of positioning a group of elements as follows:

find_elements_by_id()

find_elements_by_name()

find_elements_by_class_name()

find_elements_by_tag_name()

find_elements_by_link_text()

find_elements_by_partial_link_text()

find_elements_by_xpath()

find_elements_by_css_selector()

Appium fully inherited methods WebDriver defined, except that its extension, in order to fit with the positioning operation of the mobile object terminal.

Mobile JSON Wire Protocol by the process defined in the protocol, positioning control is more suitable for mobile devices.

  • ios uiautomation: a recursive corresponding character string (IOS-only) use UIAutomation library search elements.
  • android uiautomator: a recursive corresponding character string (Android-only) use UIAutomation Api search elements.
  • accessibility id: a recursive, Id corresponding to the local implementation option Accessibility / Name of the elements of the search string.

 For python, in the basis WebDriver method based on the increase of the following methods:

find_element_by_accessibility_id()

find_elements_by_accessibility_id()

find_element_by_android_uiautomator()

find_elements_by_android_uiautomator()

But we control query to the elements, you can find a lot of positioning methods can not be achieved.

 

Click Inspect Element, found that there are not any, and most or class, id element name.

 

So we just need to learn id, class, xpath positioned to complete the purpose of positioning elements. 

1.1 id Positioning

After several tests I found id value app has two attributes can be represented, respectively, id and resource-id.

If only one id, then use find_element_by_id to query.

# coding:utf-8
import time
from appium import webdriver

# 初始化
desired_caps = {}
# 使用哪种移动平台
desired_caps['platformName'] = 'Android'
# Android版本
desired_caps['platformVersion'] = '5.1.1'
#使用adb devices -l 查询,当有多台设备时,需要声明
desired_caps['deviceName'] = '127.0.0.1:62001'
#包名
desired_caps['appPackage'] = 'com.android.settings'
# com.tencent.mobileqq
#界面名
desired_caps['appActivity'] = '.Settings'

# 启动服务
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

driver.find_element_by_id('com.android.settings:id/search').click()

time.sleep(5)

driver.quit()

启动这个程序,我们可以看到点击了搜索按钮,进入了搜索界面。

而id值不是每个元素都会显示的,而resource-id可以查询到,但是在大多数时候resource-id是一系列元素的id,所以我们需要使用find_elements_by_id方法来查询id。

# coding:utf-8
import time
from appium import webdriver

# 初始化
desired_caps = {}
# 使用哪种移动平台
desired_caps['platformName'] = 'Android'
# Android版本
desired_caps['platformVersion'] = '5.1.1'
#使用adb devices -l 查询,当有多台设备时,需要声明
desired_caps['deviceName'] = '127.0.0.1:62001'
#包名
desired_caps['appPackage'] = 'com.android.settings'
# com.tencent.mobileqq
#界面名
desired_caps['appActivity'] = '.Settings'
# 启动服务
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

titles = driver.find_elements_by_id("com.android.settings:id/title")
for title in titles:
    print(title.text)

time.sleep(5)

driver.quit()

1.2 class定位

class一般都不止一个,所以应该需要遍历一遍得到的元素,然后缩小搜索条件来获得目标元素。

我们可以借助python中的pop()方法确定想要这一组元素中的第几个,并对它进行点击或输入操作。

pop(0) 或pop(-1):默认获得一组元素中的最后一个。

pop(n):获得一组元素中的第n-1个。

一个class元素,使用find_element_by_class_name来查询。

# coding:utf-8
import time
from appium import webdriver

# 初始化
desired_caps = {}
# 使用哪种移动平台
desired_caps['platformName'] = 'Android'
# Android版本
desired_caps['platformVersion'] = '5.1.1'
#使用adb devices -l 查询,当有多台设备时,需要声明
desired_caps['deviceName'] = '127.0.0.1:62001'
#包名
desired_caps['appPackage'] = 'com.android.settings'
# com.tencent.mobileqq
#界面名
desired_caps['appActivity'] = '.Settings'

# 启动服务
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

driver.find_element_by_id('com.android.settings:id/search').click()
driver.find_element_by_class_name("android.widget.EditText").send_keys("hello")

time.sleep(5)

driver.quit()

可以看到点击搜索按钮后出现后,在输入框输入了hello。

 

 

如果class元素有很多,就可以使用find_elements_by_class_name来查询。

# coding:utf-8
import time
from appium import webdriver

# 初始化
desired_caps = {}
# 使用哪种移动平台
desired_caps['platformName'] = 'Android'
# Android版本
desired_caps['platformVersion'] = '5.1.1'
#使用adb devices -l 查询,当有多台设备时,需要声明
desired_caps['deviceName'] = '127.0.0.1:62001'
#包名
desired_caps['appPackage'] = 'com.android.settings'
#界面名
desired_caps['appActivity'] = '.Settings'

# 启动服务
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

text_views = driver.find_elements_by_class_name("android.widget.TextView")
for text_view in text_views:
    print(text_view.text)

time.sleep(5)

driver.quit()

 

1.3 xpath 

在webdriver上xpath定位是功能强大的一种定位方式。我个人惯用于此方法来定位web页面上的元素。但是在android上要定位的是控件,而非页面元素,xpath定位的写法也会有所不同。 

# coding:utf-8
import time
from appium import webdriver

# 初始化
desired_caps = {}
# 使用哪种移动平台
desired_caps['platformName'] = 'Android'
# Android版本
desired_caps['platformVersion'] = '5.1.1'
#使用adb devices -l 查询,当有多台设备时,需要声明
desired_caps['deviceName'] = '127.0.0.1:62001'
#包名
desired_caps['appPackage'] = 'com.android.settings'
# com.tencent.mobileqq
#界面名
desired_caps['appActivity'] = '.Settings'

# 启动服务
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)#隐式等待,下一章会讲解
driver.find_element_by_id("com.android.settings:id/search").click()
driver.find_element_by_class_name("android.widget.EditText").send_keys("hello")
driver.find_element_by_xpath("//*[@content-desc='收起']").click()

time.sleep(5)

driver.quit()

就目前而言吧,我觉得appium的xpath定位很不好使用,可能是因为还不够熟练的原因吧。

# coding:utf-8
import time
from appium import webdriver

# 初始化
desired_caps = {}
# 使用哪种移动平台
desired_caps['platformName'] = 'Android'
# Android版本
desired_caps['platformVersion'] = '5.1.1'
#使用adb devices -l 查询,当有多台设备时,需要声明
desired_caps['deviceName'] = '127.0.0.1:62001'
#包名
desired_caps['appPackage'] = 'com.android.settings'
# com.tencent.mobileqq
#界面名
desired_caps['appActivity'] = '.Settings'

# 启动服务
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

elements = driver.find_elements_by_xpath("//*[contains(@text,'设')]")
for element in elements:
    print(element.text)

time.sleep(5)

driver.quit()

和webdriver一样,如果使用find_element_by_xx方法,如果传入一个没有的特征,会报NoSuchElementException的错误。如果使用find_elements_by_xx方法,如果传入一个没有的特征,不会报错,会返回一个空列表。

Guess you like

Origin www.cnblogs.com/liuhui0308/p/12015458.html