Objective
Sometimes there is no id attribute on the page, and the other attributes are not unique, usually used more is the singular (element) positioning method, encountered element attributes are not unique, you can not directly navigate to.
So we can locate a set of elements by a complex number (elements), and then take out the elements by subscript, so that we can also locate the elements.
singular and plural
1.find_element begins with 13 singular positions.
The 2.find_elements begins with 13 complex positions.
Locating a group of objects
1. Compare the results of locating elements with singular positioning find_element and plural positioning find_elements
# coding:utf-8from appium import webdriverdesired_caps = { 'platformName': 'Android', 'deviceName': '127.0.0.1:62001', 'platformVersion': '4.4.2', 'appPackage': 'com.baidu.yuedu', 'appActivity': 'com.baidu.yuedu.splash.SplashActivity', 'noReset': 'true', 'resetKeyboard': 'true', 'unicodeKeyboard': 'true' }driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)# 等主页面activity出现driver.wait_activity(".base.ui.MainActivity", 10)# 定位'搜索'按钮search = driver.find_element_by_id("com.baidu.yuedu:id/tab_search")print(search) # 打印元素对象searchs = driver.find_elements_by_id("com.baidu.yuedu:id/tab_search")print(searchs) # 打印listprint(type(searchs))
2. Locates a set of elements that return a list object
3. After positioning a group, if you want to click on the element, then first from the list through the subscript index to take out the element object, and then click on it. The subscript index is starting from 0.
# list定位driver.find_elements_by_id("com.baidu.yuedu:id/tab_search")[0].click()
Element not unique
1. Usually a page id attribute is unique, but sometimes encountered some elements do not have an id attribute, only the class attribute, usually the class attribute is not unique
2. If you want to position the first picture element, you can first use find_elements to locate a set of Image objects, and then use the subscript index [0] to remove the first one.
# 点搜索结果第一个driver.find_elements_by_class_name("android.widget.Image")[0].click()
Reference case
# coding:utf-8from Appium Import webdriverfrom selenium.webdriver.support.ui import Webdriverwaitfrom Selenium.webdriver.support Import expected_conditions as ecfrom time import sleepdesired_caps = {' Platform Name ': ' Android ', ' devicename ': ' 127.0.0.1:62001 ', ' platformversion ': ' 4.4.2 ', ' Apppackage ': ' com.baidu.yuedu ', ' appactivity ': ' com.baidu.yuedu.splash.SplashActivity ', ' NoReset ': ' true ', ' Resetkeyboard ': ' true ', ' Unicodekeyboard ': ' True '}driver = Webdriver. Remote (' Http://127.0.0.1:4723/wd/hub ', desired_caps) # etc Main Page activity appears driver.wait_activity (". Base.ui.MainActivity" , 10) # dot ' Search ' driver.find_element_by_id ("Com.baidu.yuedu:id/tab_search"). Click () # Enter "Python" driver.find_element_by_ ID ("Com.baidu.yuedu:id/full_text_search_bar_input"). Send_keys (U "Python Interface") Sleep (2) # Point Search button driver.find_element_by _id ("Com.baidu.yuedu:id/full_text_search_bar_search"). Click () sLeep (5) # Point search results First driver.find_elements_by_class_name ("Android.widget.Image") [0].click ()
Appium+python Automated 30-list positioning (find_elements)