前言

有时候页面上没有id属性,并且其它的属性不唯一,平常用的比较多的是单数(element)的定位方法,遇到元素属性不唯一,就无法直接定位到了。

于是我们可以通过复数(elements)定位,先定位一组元素,再通过下标取出元素,这样也是可以定位到元素的。

单数与复数

1.find_element开头的是13种单数定位

2.find_elements开头是13种复数定位

定位一组对象

1.对比用单数定位find_element和复数定位find_elements定位元素的结果

# coding:utf-8
from appium import webdriver
desired_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) # 打印list
print(type(searchs))

2.定位一组元素,返回的是list对象

3.定位一组之后,如果要点击该元素,那就先从list里面通过下标索引取出元素对象,再click就可以了。下标索引是从0开始。

# list定位
driver.find_elements_by_id("com.baidu.yuedu:id/tab_search")[0].click()

元素不唯一

1.通常一个页面上id属性是唯一的,但是有时候会遇到有些元素没有id属性,只有class属性,通常class属性不唯一

2.如果要定位第一个图片元素,可以先用find_elements定位一组Image对象,再通过下标索引[0]取出第一个就可以了。

# 点搜索结果第一个
driver.find_elements_by_class_name("android.widget.Image")[0].click()

参考案例

# coding:utf-8
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
desired_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) # 点'搜索'
driver.find_element_by_id("com.baidu.yuedu:id/tab_search").click() # 输入"python"
driver.find_element_by_id("com.baidu.yuedu:id/full_text_search_bar_input").send_keys(u"Python接口")
sleep(2)
# 点搜索按钮
driver.find_element_by_id("com.baidu.yuedu:id/full_text_search_bar_search").click() sleep(5)
# 点搜索结果第一个
driver.find_elements_by_class_name("android.widget.Image")[0].click()

在学习过程中有遇到疑问的,可以appium+python QQ群交流:330467341

appium+python自动化30-list定位(find_elements)的更多相关文章

  1. appium+python自动化50-生成定位对象模板templet(jinja2)

    前言 每次自己写pageobject定位元素对象太繁琐,格式都差不多,只是换个定位方法,这种就可以才有模板的方式,批量生成pageobject定位元素对象的模板 python里面生成模板有两个模块可以 ...

  2. Appium+python自动化4-元素定位uiautomatorviewer

    前言 环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作. uiautomatorviewer是androi ...

  3. Appium+python自动化12-appium元素定位

    前言 appium定位app上的元素,可以通过id,name.class这些属性定位到 一.id定位 1.appium的id属性也就是通过UI Automator工具查看的resource-id属性

  4. appium+python自动化49-yaml管理定位元素

    前言 如何高效管理定位元素,这个是很有学问的问题,也是面试必问的[以下纯属个人观点,勿喷!]. 有的人用xml管理页面定位元素,这种逼格略高,但是小编认为学习成本大,贼麻烦. 有的人提到用excel管 ...

  5. Appium+python自动化12-appium元素定位【转载】

    前言 appium定位app上的元素,可以通过id,name.class这些属性定位到 一.id定位 1.appium的id属性也就是通过UI Automator工具查看的resource-id属性

  6. Appium+python自动化4-元素定位uiautomatorviewer【转载】

    前言 环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作. uiautomatorviewer是androi ...

  7. Appium+python自动化

    名称 链接地址 Appium+python自动化8-Appium Python API(上) http://mp.weixin.qq.com/s/WvpT5oRrYY22avI95FuypQ Appi ...

  8. Appium+python自动化8-Appium Python API

    Appium+python自动化8-AppiumPython API   前言: Appium Python API全集,不知道哪个大神整理的,这里贴出来分享给大家. 1.contexts conte ...

  9. appium+python自动化52-多点触控MultiAction

    前言 MultiAction是针对多点触控操作的,是TouchAction的一个补充模块 TouchAction用法参考前面的一篇:appium+python自动化33-TouchAction 多点触 ...

  10. Appium+python自动化20-查看iOS上app元素属性

    前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道,appium的windows版自带的Inspector可以定位app上的元素 Mac版的appium1.6的 ...

随机推荐

  1. 内存保护机制及绕过方法——通过覆盖部分地址绕过ASLR

    ASLR保护机制 ASLR简介 微软在Windows Vista.2008 server.Windows 7.Windows 8等系统的发布中, 开始将ASLR作为内置的系统保护机制运行, 将系统映像 ...

  2. 词频统计 ——Java

    github地址 :https://github.com/NSDie/personal-project 一.计划表 PSP2.1 Personal Software Process Stages 预估 ...

  3. va_start、va_arg、va_end、va_copy 可变参函数

    1.应用与原理         在C语言中,有时我们无法给出一个函数参数的列表,比如: int printf(const char *format, ...); int fprintf(FILE *s ...

  4. vue.js 源代码学习笔记 ----- observe

    参考 vue 2.2.6版本 /* @flow */ //引入订阅者模式 import Dep from './dep' import { arrayMethods } from './array' ...

  5. c# winform捕获全局异常,并记录日志

    using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using ...

  6. How to choose the number oftopics/partitions in a Kafka cluster?

    How to choose the number oftopics/partitions in a Kafka cluster? 如何为一个kafka集群选择topics/partitions的数量? ...

  7. EasyPlayer RTSP播放器:一个适用于安防行业的工具利器(EasyPlayer Windows v2.0.17.0709)

    本文转自EasyDarwin开源团队成员Sword的博客:http://blog.csdn.net/swordtwelve EasyPlayer(Windows) v2.0.17.0709版本又更新发 ...

  8. EasyDSS流媒体服务器Linux emerg getpwnam("xxx") failed解决办法

    本文转自EasyDarwin开源团队Alex的博客:http://blog.csdn.net/cai6811376/article/details/73770943 EasyDSS 流媒体服务器是什么 ...

  9. [Math]PHI, the golden ratio

    PHI, the golden ratio 黄金分割比 转载自 http://paulbourke.net/miscellaneous/miscnumbers/ 1. Definition 将一个线段 ...

  10. SimpliciTI无线开发 — 快速搭建低功耗、低成本无线传感器网络

    一.初闻SimpliciTI SimpliciTI是TI在2007年宣布推出的针对小型简单RF网络的专有低功耗协议,因其简化了具体的实施操作,降低对微控制器资源的占用,故基于SimpliciTI的RF ...