Appium學習筆記15-滑動和拖拽


使用手機時會遇到下滑,拖拽等操作,這就需要用滑動的方法。

1.swipe從座標點滑動到座標點

#5個參數(起始橫座標,起始縱座標,結束橫座標,結束縱座標,滑動時長單位毫秒)
driver.swipe(start_x,start_y,end_x,end_y,duration=None)
#每次操作的期望時間與真實時間有誤差,造成了每次的滑動距離不等。
#滑動操作具有慣性,速度快,慣性大;速度慢慣性小。可能導致每次滑動的距離不等
#滑動時長越大,慣性越小,滑動距離越準確

用5秒的時間,向下滑動900個像素點,代碼:

#導入庫
from appium import webdriver
import time

desired_caps = dict()#創建字典
desired_caps['platformName'] = 'Android'#添加字典字段:手機平臺(Android、iOS)
desired_caps['platformVersion'] = '5.1'#添加字典字段:系統版本號(可從手機的設置裏面查看)
desired_caps['deviceName'] = 'myphone'#添加字典字段:設備名稱(隨便寫即可)
desired_caps['appPackage'] = 'com.android.settings'#添加字典字段:要打開的app包名
desired_caps['appActivity'] = 'com.android.settings.Settings'#添加字典字段:APP的界面名
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)#調用方法,其中'http://localhost:4723/wd/hub'即爲appium服務器地址
driver.find_element_by_id("android:id/button1").click()#根據id定位元素,彈出應用的授權框,點擊“確認”

driver.swipe(100,1000,100,100,5000)#用5秒的時間,向下滑動900個像素點
time.sleep(3)#等待3秒

driver.quit()#退出此次驅動連接

2.scroll從元素位置滑動到元素位置(有慣性)

#從一個元素滑動到另一個元素,有慣性
#2個參數(起始元素,終止元素)
driver.scroll(origin_el,des_el)

從藍牙滑動到無線網絡,代碼:

#導入庫
from appium import webdriver
import time

desired_caps = dict()#創建字典
desired_caps['platformName'] = 'Android'#添加字典字段:手機平臺(Android、iOS)
desired_caps['platformVersion'] = '5.1'#添加字典字段:系統版本號(可從手機的設置裏面查看)
desired_caps['deviceName'] = 'myphone'#添加字典字段:設備名稱(隨便寫即可)
desired_caps['appPackage'] = 'com.android.settings'#添加字典字段:要打開的app包名
desired_caps['appActivity'] = 'com.android.settings.Settings'#添加字典字段:APP的界面名
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)#調用方法,其中'http://localhost:4723/wd/hub'即爲appium服務器地址
driver.find_element_by_id("android:id/button1").click()#根據id定位元素,彈出應用的授權框,點擊“確認”

bluetooth=driver.find_element_by_xpath("//*[@text='藍牙']")
wifi=driver.find_element_by_xpath("//*[@text='無線網絡']")

driver.scroll(bluetooth,wifi)#從藍牙滑動到無線網絡
time.sleep(3)#等待3秒

driver.quit()#退出此次驅動連接

3.drag_and_drop從元素位置滑動到元素位置(無慣性)

#從一個元素滑動到另一個元素,沒有慣性
#2個參數(起始元素,終止元素)
driver.drag_and_drop(origin_el,des_el)

從藍牙滑動到無線網絡,代碼:

#導入庫
from appium import webdriver
import time

desired_caps = dict()#創建字典
desired_caps['platformName'] = 'Android'#添加字典字段:手機平臺(Android、iOS)
desired_caps['platformVersion'] = '5.1'#添加字典字段:系統版本號(可從手機的設置裏面查看)
desired_caps['deviceName'] = 'myphone'#添加字典字段:設備名稱(隨便寫即可)
desired_caps['appPackage'] = 'com.android.settings'#添加字典字段:要打開的app包名
desired_caps['appActivity'] = 'com.android.settings.Settings'#添加字典字段:APP的界面名
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)#調用方法,其中'http://localhost:4723/wd/hub'即爲appium服務器地址
driver.find_element_by_id("android:id/button1").click()#根據id定位元素,彈出應用的授權框,點擊“確認”

bluetooth=driver.find_element_by_xpath("//*[@text='藍牙']")
wifi=driver.find_element_by_xpath("//*[@text='無線網絡']")

driver.drag_and_drop(bluetooth,wifi)#從藍牙滑動到無線網絡
time.sleep(3)#等待3秒

driver.quit()#退出此次驅動連接
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章