习题1:在首页搜索框中输入“小米手机”,点击搜索按钮 点击搜索到的第一个商品,进入商品详情页 点击“加入购物车”,购买数量为2件,并确定 暂停5秒,关闭驱动 要求:设置隐式等待10秒
import time
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
server = "http://127.0.0.1:4723/wd/hub"
# tpshop配置参数
desired_caps = {
"platformName": "Android",# 系统型号
"deviceName": "xxx",#设备号
"autoGrantPermissions": True,#是否自动授权
"appPackage": "com.tpshop.malls",#APP的包名
"appActivity": ".SPMainActivity"#app打开的第一个界面
}
driver = webdriver.Remote(server, desired_caps)
# 添加隐式等待
driver.implicitly_wait(10)
# 在首页搜索框中输入“小米手机”,点击搜索按钮
driver.find_element(MobileBy.CLASS_NAME,'android.widget.EditText').click()
driver.find_element(MobileBy.CLASS_NAME,'android.widget.EditText').send_keys('小米手机')
driver.find_element(MobileBy.CLASS_NAME,'android.widget.TextView').click()
# 点击搜索到的第一个商品,进入商品详情页
#定位综合元素点击元素的相对于坐标偏移量 x=100, y=160
return_el = driver.find_element(MobileBy.XPATH, "//*[@text='综合']")
TouchAction(driver).tap(return_el, x=100, y=160).perform()
# 点击“加入购物车”,
driver.find_element(MobileBy.ID,'com.tpshop.malls:id/add_cart_tv').click()
# 购买数量为2件,clear清除1;send_keys输入2
driver.find_element(MobileBy.CLASS_NAME,'android.widget.EditText').clear()
driver.find_element(MobileBy.CLASS_NAME,'android.widget.EditText').send_keys('2')
#并确定加入购物车
driver.find_element(MobileBy.CLASS_NAME,'android.widget.TextView').click()
time.sleep(1)
driver.quit()
习题2:点击“我的”进入我的页面,点击用户头像进入登录页面 输入账号、密码,同意协议,点击登录按钮 登录成功之后,获取当前用户的昵称并打印 暂停5秒,关闭驱动 要求:设置隐式等待10秒
import time
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
server = "http://127.0.0.1:4723/wd/hub"
# tpshop配置参数
desired_caps = {
"platformName": "Android",
"deviceName": "xxx",
"autoGrantPermissions": True,
"appPackage": "com.tpshop.malls",
"appActivity": ".SPMainActivity"
}
driver = webdriver.Remote(server, desired_caps)
# 添加隐式等待
driver.implicitly_wait(10)
time.sleep(2)
# 点击我的
driver.find_element(MobileBy.XPATH, "//*[@text='我的']").click()
# 点击用户头像进入登录页面
driver.find_element(MobileBy.ID, "com.tpshop.malls:id/head_img").click()
#输入数据输入账号、密码,同意协议,点击登录按钮
driver.find_element(MobileBy.XPATH, "//*[@text='请输入账号']").send_keys("123456@qq.com")
driver.find_element(MobileBy.ID,"com.tpshop.malls:id/pwd_et").send_keys("123456")
driver.find_element(MobileBy.ID,"com.tpshop.malls:id/agree_btn").click()
driver.find_element(MobileBy.ID,"com.tpshop.malls:id/login_tv").click()
# # 获取登录成功信息,打印账号消息
el_text=driver.find_element(MobileBy.ID,"com.tpshop.malls:id/nick_name_tv").text
print('账号成功登录',el_text)
time.sleep(1)
driver.quit()
习题3:点击“我的”进入我的页面 暂停2秒,向上滑动一次屏幕 点击"地址管理"进入地址管理页面 暂停5秒,关闭驱动 要求:设置隐式等待10秒
import time
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
server = "http://127.0.0.1:4723/wd/hub"
# tpshop配置参数
desired_caps = {
"platformName": "Android",
"deviceName": "xxx",
"autoGrantPermissions": True,
"appPackage": "com.tpshop.malls",
"appActivity": ".SPMainActivity"
}
driver = webdriver.Remote(server, desired_caps)
# 添加隐式等待
driver.implicitly_wait(10)
time.sleep(2)
#点击“我的”进入我的页面
driver.find_element(MobileBy.XPATH, "//*[@text='我的']").click()
#向上滑动一次屏幕
dz=driver.find_element(MobileBy.XPATH,'//*[@text="地址管理"]')
hb=driver.find_element(MobileBy.XPATH,'//*[@text="海报"]')
driver.scroll(dz,hb)#向上滑动
# 起始地址管理的位置到终点海报的位置所以是向上滑动
print(dz.size,hb.size)
#点击"地址管理"进入地址管理页面
driver.find_element(MobileBy.XPATH, "//*[@text='地址管理']").click()
time.sleep(1)
driver.quit()
滑动找元素
import time
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
server = "http://127.0.0.1:4723/wd/hub"
# 网易新闻配置参数
desired_caps = {
"platformName": "Android",
"deviceName": "any",
"autoGrantPermissions": True,
"appPackage": "com.netease.newsreader.activity",
"appActivity": "com.netease.nr.phone.main.MainActivity"
}
driver = webdriver.Remote(server, desired_caps)
# 添加隐式等待
driver.implicitly_wait(10)
# ①.模拟手机滑动频道,找到‘健康’频道,并点击
# 1 确定滑动起始点和终点坐标
channel = driver.find_element(MobileBy.CLASS_NAME, "android.widget.HorizontalScrollView")
# 起始点:start_x = 频道区域的宽*0.8 start_y = 频道区域的y坐标+频道区域的高*0.5
start_x = channel.size['width']*0.8
start_y = channel.location['y'] + channel.size['height']*0.5
# 终止点:end_x = 频道区域的宽*0.2 end_y = 频道区域的y坐标+频道区域的高*0.5
end_x = channel.size['width']*0.2
end_y = channel.location['y'] + channel.size['height']*0.5
#打印起始点和终点坐标位置
print(f"start_x={start_x}, start_y={start_y}")
print(f"end_x={end_x}, end_y={end_y}")
# 2 滑动找元素
while True:
try:#循环找到元素后点击跳出循环
el = driver.find_element(MobileBy.XPATH, "//*[@text='健康']")
el.click()
break
except Exception as e:#没有找到目标元素开始循环滑动
print("没有找到目标元素")
driver.swipe(start_x=start_x, start_y=start_y, end_x=end_x, end_y=end_y)
time.sleep(1)
driver.quit()
封装滑动动作
import time
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
# 封装一个边滑动边查找元素的方法
def swipe_find_el(driver, loc, fx='up'):
"""
:param driver: 驱动对象
:param loc: 查找元素的定位信息()
:param fx: 滑动方向
:return: 返回找到元素
"""
# 1 计算屏幕的分辨率大小
# 滑动的坐标计算
w = driver.get_window_size()["width"] # 屏幕的宽度
h = driver.get_window_size()["height"] # 屏幕的高度
if fx == 'up':
start_x = end_x = w/2
start_y = h * 0.9
end_y = h * 0.1
elif fx == 'down':
start_x = end_x = w/2
start_y = h * 0.1
end_y = h * 0.9
elif fx == 'left':
start_y = end_y = h/2
start_x = w * 0.9
end_x = w * 0.1
else:
start_y = end_y = h/2
start_x = w * 0.1
end_x = w * 0.9
while True:
start_page=driver.page_source
try:
el = driver.find_element(*loc).click()
print("找到目标元素~!")
return el
except Exception as e:
print("没有找到目标元素~!需要进行一次滑动再找元素")
driver.swipe(start_x=start_x, start_y=start_y, end_x=end_x,
end_y=end_y, duration=1500)
if start_page==driver.page_source:
print("在最后也没有找到目标元素~!")
return None


