如果你还想从头学起Appium,可以看看这个系列的文章哦!

https://www.cnblogs.com/poloyy/category/1693896.html

快速入门栗子:boss直聘 app

环境要求:Android

栗子一:定位【自动化测试】

代码

test = driver.find_element_by_class_name("android.widget.TextView")
print(test.text) test = driver.find_element_by_xpath("//*[@resource-id='com.hpbr.bosszhipin:id/title_container']/android.widget.FrameLayout/android.widget.TextView")
print(test.text) test = driver.find_element_by_xpath("//*[contains(@text,'自动化测试')]")
print(test.text)

输出结果

自动化测试
自动化测试
自动化测试

栗子二:定位【附近】

代码

test = driver.find_element_by_id("com.hpbr.bosszhipin:id/tv_tab_label")
print(test.text) test = driver.find_element_by_xpath("//*[@resource-id='com.hpbr.bosszhipin:id/tv_tab_label']")
print(test.text) test = driver.find_element_by_xpath("//*[contains(@text,'附近')]")
print(test.text)

输出结果

推荐
推荐
附近

为啥会有两个推荐?

  • 很明显因为【推荐、附近】两个 tab 的 resource-id 都是一样的,而 find_element_by 只返回第一个匹配到的元素
  • 这个时候可以将代码改成 find_elements_by 匹配多个元素,再根据索引去匹配【附近】
test = driver.find_elements_by_id("com.hpbr.bosszhipin:id/tv_tab_label")[1]
print(test.text) test = driver.find_elements_by_xpath("//*[@resource-id='com.hpbr.bosszhipin:id/tv_tab_label']")[1]
print(test.text)

输出结果

附近
附近

实战栗子:完整的流程

  1. 进入boss app
  2. 点击右上角的放大镜icon
  3. 点击输入框
  4. 输入“软件测试”
  5. 点击第一个结果
  6. 打印第一屏的地区、工作经验、学历
#!/usr/bin/env python
# -*- coding: utf-8 -*- """
__title__ =
__Time__ = 2020-04-15 21:59
__Author__ = 小菠萝测试笔记
__Blog__ = https://www.cnblogs.com/poloyy/
""" # 导包
from appium import webdriver # 准备自动化配置信息
desired_caps = {
# 移动设备平台
'platformName': 'Android',
# 平台OS版本号,写整数位即可
'plathformVersion': '',
# 设备的名称--值可以随便写
'deviceName': 'test0106',
# 提供被测app的信息-包名,入口信息
'appPackage': 'com.hpbr.bosszhipin',
'appActivity': '.module.launcher.WelcomeActivity',
# 如果被测应用没有安装到手机上,可以指定apk的在电脑上路径
# 'app':r'D:\apk\xxx.apk',
# 确保自动化之后不重置app
'noReset': True,
# 设置session的超时时间,单位秒
'newCommandTimeout': 6000,
# 如果不想每次都安装UI2驱动,可以这么设置
'skipServerInstallation': True
} # 初始化driver对象-用于控制手机
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(10) # 稳定元素 # 点击放大镜
driver.find_element_by_xpath('//*[@resource-id="com.hpbr.bosszhipin:id/ly_menu"]/android.widget.RelativeLayout[2]/.').click() # 搜索框输入职位信息
search_input = driver.find_element_by_id('com.hpbr.bosszhipin:id/et_search')
search_input.send_keys('软件测试') # 输入参数 # 选择符合条件的第一个搜索结果
driver.find_element_by_id('com.hpbr.bosszhipin:id/tv_filtered_name').click() # 获取当前页面所有职位信息元素
job_msg = driver.find_elements_by_id('com.hpbr.bosszhipin:id/view_job_card') for job in job_msg:
# 输出岗位名称
name = job.find_element_by_id('com.hpbr.bosszhipin:id/tv_position_name')
# print(name.text)
# 输出薪资
salray = job.find_element_by_id('com.hpbr.bosszhipin:id/tv_salary_statue')
# print(salray.text)
# 输出公司名称
# 找到元素返回包含一个元素的列表,找不到就返回空列表
company = job.find_elements_by_id('com.hpbr.bosszhipin:id/tv_company_name')
# 避免屏幕遮挡了公司名,查到不到目标元素,设置一共默认值
company_text = '空'
# 当找打company元素的时候,就使用该元素的文本
if company:
company_text = company[0].text print('%s|%s|%s' % (name.text, salray.text, company_text)) # 点击第一个搜索结果
job_msg[0].click() # 获取职位名称下面的信息:地区、工作年限、学历、工作性质
location = driver.find_element_by_id('tv_required_location').text work_exp = driver.find_element_by_id('tv_required_work_exp').text degree = driver.find_element_by_id('tv_required_degree').text print(f'地区:{location}|工作经验:{work_exp}|学历:{degree}') driver.quit()

输出结果

Appium自动化(9) - appium元素定位的快速入门的更多相关文章

  1. appium自动化测试之UIautomatorviewer元素定位

    appium自动化测试之UIautomatorviewer元素定位 标签(空格分隔): uiautomatorviewer元素定位 前面的章节,已经总结了怎么搭建环境,怎样成功启动一个APP了,这里具 ...

  2. python+Appium自动化:H5元素定位

    问题思考 在混合开发的App中,经常会有内嵌的H5页面.那么这些H5页面元素该如何进行定位操作呢? 解决思路 针对这种场景直接使用前面所讲的方法来进行定位是行不通的,因为前面的都是基于Andriod原 ...

  3. python+Appium自动化:id元素定位

    元素定位 与web自动化一样,app自动化元素定位也是非常重要的一环,,appium也是提供了很多元素定位的方法,比如:id.name.class.层级定位等等. 元素定位方式 id name cla ...

  4. python+Appium自动化:各种元素定位方法

    name定位 driver.find_element_by_name('飞利浦净水').click() 测试结果报错:selenium.common.exceptions.InvalidSelecto ...

  5. 元素定位-----Selenium快速入门(二)

    一.eclipse设置 工欲善其事必先利其器,在说元素定位之前,先来设置下eclipse. 首先放大一下字体,点击windows-preferences 其次,eclipse对于java的智能提示默认 ...

  6. 5、通过Appium Desktop实现页面元素定位

    之前我们已经安装了Appium Desktop,下面就让我们使用Appium Desktop实现页面元素定位 1.首先我们打开Appium Desktop,进入如下界面,点击Start Server ...

  7. 『与善仁』Appium基础 — 17、元素定位工具(一)

    目录 1.uiautomatorviewer介绍 2.uiautomatorviewer工具打开方式 3.uiautomatorviewer布局介绍 4.uiautomatorviewer工具的使用 ...

  8. 『与善仁』Appium基础 — 18、元素定位工具(二)

    目录 1.Appium Inspector介绍 2.Appium Inspector打开方式 3.Appium Inspector布局介绍 4.Appium Inspector工具的配置 5.Appi ...

  9. Appium学习笔记4_元素定位方法

    Appium之元素定位,如果对Android上如何使用工具获取页面元素有问题的,请转战到这:http://www.cnblogs.com/taoSir/p/4816382.html. 下面主要是针对自 ...

随机推荐

  1. Oracle 11g 精简客户端

    通常开发人员会装上一个 oracle客户端,但一般不会在自己的机器上安装Oracle database Oracle 客户端安装体积很大,但是装上去了基本上就用2个功能:TNS配置服务名和sqlplu ...

  2. java中functional interface的分类和使用

    目录 简介 Functional Interface Function:一个参数一个返回值 BiFunction:接收两个参数,一个返回值 Supplier:无参的Function Consumer: ...

  3. Gym 101194F Mr. Panda and Fantastic Beasts

    #include<bits/stdc++.h> using namespace std; #define ms(arr,a) memset(arr,a,sizeof arr) #defin ...

  4. java 之 servlet简介

    Servlet 是什么? Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间 ...

  5. INTERVIEW #4

    120min, 5题.本菜鸡怒跪. 1.变身程序员 (读取时可以按行读取,直到读到空行为止,再对读取过的所有行做转换处理) 输出描述:如果能将所有的产品经理变成程序员,输出最小的分钟数:如果不能将所有 ...

  6. 数学--数论--POJ281(线性同余方程)

    埃琳娜(Elina)正在阅读刘如家(Rujia Liu)写的书,其中介绍了一种表达非负整数的奇怪方法.方式描述如下: 选择k个不同的正整数a 1,a 2,-,a k.对于一些非负米,把它由每一个我(1 ...

  7. Floyd —Warshall(最短路及其他用法详解)

    一.多元最短路求法 多元都求出来了,单源的肯定也能求. 思想是动态规划的思想:从任意节点A到任意节点B的最短路径不外乎2种可能,1是直接从A到B,2是从A经过若干个节点X到B.所以,我们假设Dis(A ...

  8. Prometheus monitor RabbitMQ

    Install docker-compose sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2 ...

  9. 2-JVM内存模型

    内存模型 方法区 JDK1.7 之前包含1.7 将方法区称为 Perm Space 永久代 JDK1.8之后包含1.8 将方法区称为 MetaSpace 元空间. 堆(分配内存会大一些) 分配对象.n ...

  10. M - Little Pony and Harmony Chest 状压dp

    M - Little Pony and Harmony Chest 怎么感觉自己越来越傻了,都知道状态的定义了还没有推出转移方程. 首先这个a的范围是0~30   这里可以推出 b数组的范围 0~60 ...