最近在学习自动化框架appium,网上找一些API相关资料整理了一下

1.find_element_by_id

find_element_by_id(self, id_):

Finds element within this element's children by ID(通过元素的ID定位元素)

:Args: - id_ - ID of child element to locate.

用法 driver. find_element_by_id(“id”)

find_element_by_id方法,是对于那些有id而且可以互相区分的控件的操作使用,一般通过android sdk 的tools路径下的uiautomatorviewer.bat自带工具来获取,

使用这个工具首先要确保前面环境配置ok,然后确定测试机器或android模拟器处于连接状态(cmd上输入adb devices),下面是使用uiautomatorviewer.bat工具

获取resource-id

代码举例:

#点击天气应用
driver.find_element_by_id('com.huawei.android.totemweather:id/mulan_widget_currentweather_smallicon').click()  

2.find_element_by_xpath

find_element_by_xpath(self, xpath):

Finds element by xpath(通过Xpath定位元素,详细方法可参阅http://www.w3school.com.cn/xpath/ )

```
:Args: xpath - xpath of element to locate. "//input[@class='myelement']" Note: The base path will be relative to this element's location. This will select the first link under this element. :: myelement.find_elements_by_xpath(".//a") However, this will select the first link on the page. :: myelement.find_elements_by_xpath("//a")
```

用法 find_element_by_xpath(“//*”)

find_element_by_xpath方法也需要使用uiautomatorviewer.bat工具来定位控件,如下图所示右边相机控件id为空,这时就需要使用xpath来定位元素了,

我们可以使用xpath用text和index来定位元素,可以这样定位相机:

driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'相机')]")

driver.find_element_by_xpath("//android.widget.TextView[contains(@index,8)]")

如下图的图库那个控件index和天气温度控件是相同的,都是index=7,就只能用text或text与index结合了,如:

driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'图库')]")

driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'图库')and@index='7']")

上面的代码就可以这样写

# -*-coding=utf-8 -*-

from appium import webdriver
import time
desired_caps = {
'platformName' : 'Android',
'deviceName' : '76P4C15813005463',
'platformVersion' : '5.1',
#测试apk包名
'appPackage' : 'com.huawei.android.launcher',
#测试apk的launcherActivity
'appActivity' : '.Launcher',
}
#进入android系统launcher
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep(5)
#从launcher主界面进入相机应用并退出的两种方法
driver.keyevent('3')
#xpath使用text定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'相机')]").click()
time.sleep(2)
driver.keyevent('3')
time.sleep(2)
#xpath使用index定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@index,8)]").click()
time.sleep(2)
driver.keyevent('3')
time.sleep(2) #从launcher主界面进入图库应用并退出的两种方法
#xpath使用text定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'图库').click()
time.sleep(2)
driver.keyevent('3')
time.sleep(2)
#xpath使用text与index一起定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'图库')and@index='7']").click()
time.sleep(5)
driver.keyevent('3')
time.sleep(2) driver.quit()

 3. find_element_by_name

find_element_by_name(self, name):

Finds element within this element's children by name【通过元素Name定位(元素的名称属性text)】

:Args: - name - name property of the element to find.

用法: driver.find_element_by_name(“name”)

find_elements_by_name方法的控件元素也需要使用uiautomatorviewer.bat工具来定位,如上面例子中带有text信息的控件就可以直接使用,如

driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'相机')]")

driver.find_element_by_xpath("//android.widget.TextView[contains(@index,8)]") 

可以使用

driver.find_element_by_name('相机')

脚本如下

from appium import webdriver
import time
desired_caps = {
'platformName' : 'Android',
'deviceName' : '76P4C15813005463',
'platformVersion' : '5.1',
#测试apk包名
'appPackage' : 'com.huawei.android.launcher',
#测试apk的launcherActivity
'appActivity' : '.Launcher',
}
#进入android系统launcher
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
time.sleep(5) #name方法进入相机应用
time.sleep(2)
driver.find_element_by_name("相机").click()
time.sleep(5) driver.quit()

4. find_element_by_class_name

find_element_by_class_name(self, name):

Finds element within this element's children by class name(通过元素class name属性定位元素 )

:Args: - name - class name to search for.

用法 driver. find_element_by_class_name(“android.widget.LinearLayout”)

find_element_by_class_name方法其实很不实用,一般控件的class基本相同,如下图上面的应用控件class都是android.widget.TextView,所以不好区分元素。

 5. find_element_by_link_text

find_element_by_link_text(self, link_text):

Finds element within this element's children by visible link text.
通过元素可见链接文本定位
:Args:
- link_text - Link text string to search for.
用法 driver.find_element_by_link_text(“text”)  

6. find_elements_by_link_text

find_element_by_link_text(self, link_text):

Finds a list of elements within this element's children by visible link text
通过元素可见链接文本定位,含有该属性的所有元素
:Args:
- link_text - Link text string to search for.
用法 driver.find_elements_by_link_text(“text”)

7. find_element_by_partial_link_text

find_element_by_partial_link_text(self, link_text):

 8. find_element_by_tag_name

find_element_by_tag_name(self, name):

Finds element within this element's children by tag name.
通过查找html的标签名称定位元素
:Args:
- name - name of html tag (eg: h1, a, span)
用法 driver.find_element_by_tag_name(“name”) 

9. find_element_by_css_selector

find_element_by_css_selector(self, css_selector):

‘Finds element within this element's children by CSS selector.
通过CSS选择器定位元素
:Args:
- css_selector - CSS selctor string, ex: 'a.nav#home'  

10. find_element_by_ios_uiautomation

find_element_by_ios_uiautomation(self, uia_string):

Finds an element by uiautomation in iOS.
通过iOS uiautomation查找元素
:Args:
- uia_string - The element name in the iOS UIAutomation library :Usage:
driver.find_element_by_ios_uiautomation('.elements()[1].cells()[2]')
用法dr. find_element_by_ios_uiautomation(‘elements’)  

 11. find_element_by_accessibility_id

find_element_by_accessibility_id(self, id):

Finds an element by accessibility id.
通过accessibility id查找元素
:Args:
- id - a string corresponding to a recursive element search using the
Id/Name that the native Accessibility options utilize :Usage:
driver.find_element_by_accessibility_id()
用法driver.find_element_by_accessibility_id(‘id’)

学习地址:

http://blog.csdn.net/bear_w/article/details/50330565

http://blog.csdn.net/bear_w/article/details/50330565

APPIUM API整理(python)---元素查找的更多相关文章

  1. APPIUM API整理(python)---其他辅助类

    App运行类 1.current_activity current_activity(self): 用法: print(driver.current_activity()) Retrieves the ...

  2. APPIUM API整理(python)---操作类

    前言:android手机大家都很熟悉,操作有按键.触摸.点击.滑动等,各种操作方法可以通过api的方法来实现. 参考博文:http://blog.csdn.net/bear_w/article/det ...

  3. (转载)中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...

  4. 中文Appium API 文档

    该文档是Testerhome官方翻译的源地址:https://github.com/appium/appium/tree/master/docs/cn官方网站上的:http://appium.io/s ...

  5. 移动端自动化测试(二)之 Appium常用的API(python)函数介绍

    上一章节已经介绍了Appium的环境搭建,其实只要掌握了Appium的工作原理,前期的准备工作和安装过程是比较简单的.那么当我们搭建好Appium环境后接下来做些什么呢?通常思路是开始appium的第 ...

  6. Python+Appium自动化测试(9)-自动选择USB用于传输文件(不依赖appium对手机页面元素进行定位)

    一,问题 app自动化测试使用Android真机连接电脑时,通常会遇到两种情况: 1.测试机连接电脑会弹窗提示USB选项,选择USB用于"传输文件",有些手机不支持设置默认USB选 ...

  7. 篇4 安卓app自动化测试-Appium API进阶

    篇4                 安卓app自动化测试-Appium API进阶 --lamecho辣么丑 1.1概要 大家好! 我是lamecho(辣么丑),今天是<安卓app自动化测试& ...

  8. appium API接口

    appium API接口 标签(空格分隔): appium常用api 1.contexts contexts(self) 返回当前会话的上下文,使用可以识别H5页面的控件: driver.contex ...

  9. WebDriver元素查找方法摘录与总结

    WebDriver元素查找方法摘录与总结 整理By:果冻迪迪 selenium-webdriver提供了强大的元素定位方法,支持以下三种方法. • 单个对象的定位方法 • 多个对象的定位方法 • 层级 ...

随机推荐

  1. Chem 3D软件可以改变背景吗

    化学绘图过程中常常需要绘制三维结构的图形,Chem 3D软件是ChemOffice套件中专门用于绘制三维结构的组件.用过它的用户会发现,其背景颜色通常都默认为深蓝色,但是不是每个场景都适合用深蓝色的背 ...

  2. html空格字符

    一.使用全角空格IDEOGRAPHIC SPACE----  Encodings HTML Entity (decimal)   HTML Entity (hex)   How to type in ...

  3. Request的属性和防止图片被盗链

    Request.AppRelativeCurrentExecutionFilePath,获取当前执行请求相对于应用根目录的虚拟路径,以~开头,比如"~/default.ashx" ...

  4. 【BZOJ2324】[ZJOI2011]营救皮卡丘 有上下界费用流

    [BZOJ2324][ZJOI2011]营救皮卡丘 Description 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的 ...

  5. 《从零开始学Swift》学习笔记(Day 62)——Core Foundation框架之内存托管对象与非托管对象

    原创文章,欢迎转载.转载请注明:关东升的博客 内存托管对象 Swift中调用Core Foundation函数获得对象时候,对象分为:内存托管对象和内存非托管对象. 内存托管对象就是由编译器帮助管理内 ...

  6. resin配置

    第一种方式:      在本机调试Resin服务,一般是在Eclipse IDE中进行,这样即可以快速的在开发环境中启动/终止Resin,又可以很方便的Debug程序.跟踪异常栈.    在任何版本的 ...

  7. 3年工作经验的Java程序员面试经

    转发自用~ 一.Java基础部分 1.使用length属性获取数组长度,public.private.protected.friendly区别 2.Collection和Collections区别 3 ...

  8. 使用maven搭建SSM框架

    使用maven搭建SSM框架,首先得准备好maven环境. 搭建maven环境 第一步:下载maven http://maven.apache.org/download.cgi 下载后解压就可以了. ...

  9. python文件上传工具实现

    0x00 之前验收waf模块webshell效果,组网pc--waf--webserver,收集网络上的webshell样本,进行上传测试.由于数量较多8000+个样本, 只好写了个工具进行验收. w ...

  10. PHP中Trait详解及其应用

    w PHP中Trait详解及其应用 - 开发者日常 - SegmentFaulthttps://segmentfault.com/a/1190000008009455