import time
from appium import webdriver
desired_caps = {}
desired_caps['platformName'] = 'Android' # android的apk还是IOS的ipa
desired_caps['platformVersion'] = '4.4.2' # android系统的版本号
desired_caps['deviceName'] = '127.0.0.1:62001' # 手机设备名称,通过adb devices 查看
desired_caps['appPackage'] = 'com.jd.app.reader' # apk的包名
desired_caps['appActivity'] = 'com.jingdong.app.reader.logo.JdLogoActivity' # apk的launcherActivity
desired_caps['unicodeKeyboard'] = True # 使用unicodeKeyboard的编码方式来发送字符串
desired_caps['resetKeyboard'] = True # # 将键盘给隐藏起来
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) ##启动服务器地址,后面跟的是手机信息
# 跳过广告页:
button = driver.find_element_by_id("com.jd.app.reader:id/count_down_time")
print(111, button)
button.click()
# 休眠:
# time.sleep(10)
# 点击书城:
driver.find_element_by_android_uiautomator('new UiSelector().text("书城")').click()
time.sleep(10)
# # 找有声推荐框:
res1 = driver.find_element_by_class_name("android.widget.ListView")
print(222, res1)
# 休眠:
time.sleep(10)
# 找到每个小框:
res2 = res1.find_element_by_class_name("android.widget.Image")
print(333, res2)
# 休眠:
# time.sleep(10)
# 点击:
# res2.click()
# 点击顶部的输入框
driver.find_element_by_id('com.jd.app.reader:id/mSearchLayout').send_keys()
time.sleep(1)
# 在新的搜索页面输入值
driver.find_element_by_id('com.jd.app.reader:id/mSearchKeyInput').send_keys('上古卷轴')
time.sleep(1)
# 有了值,就可以点击搜索按钮进行搜索
driver.find_element_by_id('com.jd.app.reader:id/mSearchBtn').click()
# 隐式等待
driver.implicitly_wait(10)
driver.find_element_by_id('com.jd.app.reader:id/main_tab_bookshelf').click()
# 显式等待,直到找到了这个标签
WebDriverWait(driver, 10).until(driver.find_element_by_id('com.jd.app.reader:id/main_tab_bookcity'))
driver.find_element_by_id('com.jd.app.reader:id/main_tab_bookcity').click()
# 截屏:
try:
# 根据一个不存在的 id 定位
driver.find_element_by_id("xxxxxxxxx").click()
except Exception as e:
print(e) # Message: An element could not be located on the page using the given search parameters.
driver.save_screenshot('./error.png')
# 滑动:driver.swipe
def swipeUp(driver, t=500, n=1):
'''向上滑动屏幕'''
l = driver.get_window_size()
x1 = l['width'] * 0.5 # x坐标
y1 = l['height'] * 0.75 # 起始y坐标
y2 = l['height'] * 0.25 # 终点y坐标
for i in range(n):
driver.swipe(x1, y1, x1, y2, t)
def swipeDown(driver, t=500, n=1):
'''向下滑动屏幕'''
l = driver.get_window_size()
x1 = l['width'] * 0.5 # x坐标
y1 = l['height'] * 0.25 # 起始y坐标
y2 = l['height'] * 0.75 # 终点y坐标
for i in range(n):
driver.swipe(x1, y1, x1, y2, t)
def swipLeft(driver, t=500, n=1):
'''向左滑动屏幕'''
l = driver.get_window_size()
x1 = l['width'] * 0.75
y1 = l['height'] * 0.5
x2 = l['width'] * 0.25
for i in range(n):
driver.swipe(x1, y1, x2, y1, t)
def swipRight(driver, t=500, n=1):
'''向右滑动屏幕'''
l = driver.get_window_size()
x1 = l['width'] * 0.25
y1 = l['height'] * 0.5
x2 = l['width'] * 0.75
for i in range(n):
driver.swipe(x1, y1, x2, y1, t)
if __name__ == '__main__':
time.sleep(4)
swipLeft(driver, n=2)
time.sleep(4)
swipRight(driver, n=2)
time.sleep(4)
swipeUp(driver, n=2)
time.sleep(4)
swipeDown(driver, n=2)
# 尺寸:
print(driver.get_window_size()) # {'width': 720, 'height': 1280}
print(driver.get_window_size()['width']) # 720
print(driver.get_window_size()['height']) # 1280
# 触摸操作:TouchAction
class TouchAction(object):
def __init__(self, driver=None):
self._driver = driver
self._actions = []
def tap(self, element=None, x=None, y=None, count=1):
# 模拟手指触摸屏
pass
def press(self, el=None, x=None, y=None):
# 短按:模拟手指按住一个元素,或者坐标
pass
def long_press(self, el=None, x=None, y=None, duration=1000):
# 长按:模拟按住一个元素,或者坐标
pass
def wait(self, ms=0):
# 按住元素后的等待时间
pass
def move_to(self, el=None, x=None, y=None):
# 移动手指到另外一个元素,或者坐标,注意这里坐标不是绝对坐标,是偏移量
pass
def release(self):
# 释放手指
pass
def perform(self):
# 执行前面的动作
pass
# 九宫格示例:Z形滑动
# 多点触控:MultiAction