swipe介紹
1.查看源碼語法,起點和終點四個坐標參數,duration是滑動屏幕持續的時間,時間越短速度越快。默認為None可不填,一般設置500-1000毫秒比較合適。
swipe(self, start_x, start_y, end_x, end_y, duration=None)
Swipe from one point to another point, for an optional duration.
從一個點滑動到另外一個點,duration是持續時間
:Args:
- start_x - 開始滑動的x坐標
- start_y - 開始滑動的y坐標
- end_x - 結束點x坐標
- end_y - 結束點y坐標
- duration - 持續時間,單位毫秒
:Usage:
driver.swipe(100, 100, 100, 400)
2.手機從左上角開始為0,橫着的是x軸,豎着的是y軸

獲取坐標
1.由於每個手機屏幕的分辨率不一樣,所以同一個元素在不同手機上的坐標也是不一樣的,滑動的時候坐標不能寫死了。可以先獲取屏幕的寬和高,再通過比例去計算。
# coding:utf-8
from appium import webdriver
desired_caps = {
'platformName': 'Android',
'deviceName': '30d4e606',
'platformVersion': '4.4.2',
# apk包名
'appPackage': 'com.taobao.taobao',
# apk的launcherActivity
'appActivity': 'com.taobao.tao.welcome.Welcome'
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 獲取屏幕的size
size = driver.get_window_size()
print(size)
# 屏幕寬度width
print(size['width'])
# 屏幕高度width
print(size['height'])
2.運行結果:
{u'width': 720, u'height': 1280}
720
1280
封裝滑動方法
1.把上下左右四種常用的滑動方法封裝,這樣以后想滑動屏幕時候就能直接調用了
參數1:driver
參數2:t是持續時間
參數3:滑動次數
2.案例參考
# coding:utf-8
from appium import webdriver
from time import sleep
desired_caps = {
'platformName': 'Android',
'deviceName': '30d4e606',
'platformVersion': '4.4.2',
# apk包名
'appPackage': 'com.taobao.taobao',
# apk的launcherActivity
'appActivity': 'com.taobao.tao.welcome.Welcome'
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
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__":
print(driver.get_window_size())
sleep(5)
swipLeft(driver, n=2)
sleep(2)
swipRight(driver, n=2)
在學習過程中有遇到疑問的,可以appium+python QQ群交流:330467341
