Appium中scroll和drag_and_drop根據(jù)元素位置滑動(dòng)
背景
我們?cè)诓僮鰽PP應(yīng)用時(shí),有些需要從一個(gè)元素滑動(dòng)到另外一個(gè)元素時(shí),這時(shí)候我們無(wú)法確定坐標(biāo),所以swipe 根據(jù)坐標(biāo)滑動(dòng)方式就無(wú)法使用了,如下圖:從 課堂直播 上滑到 直播公開(kāi)課 位置
這時(shí)候我們就需要使用其他滑動(dòng)方式,我們想到可以根據(jù)元素進(jìn)行滑動(dòng),Appium 里面根據(jù)元素來(lái)進(jìn)行滑動(dòng)的方式主要方法為 scroll 和 drag_and_drop
scroll 介紹
說(shuō)明
從一個(gè)元素滾動(dòng)到另一個(gè)元素,只能是兩個(gè)元素之間的滑動(dòng)。
方法詳情
def scroll(self: T, origin_el: WebElement, destination_el: WebElement, duration: Optional[int] = None) -> T: """Scrolls from one element to another Args: origin_el: the element from which to being scrolling destination_el: the element to scroll to duration: a duration after pressing originalEl and move the element to destinationEl. Default is 600 ms for W3C spec. Zero for MJSONWP. Usage: driver.scroll(el1, el2) Returns: Union['WebDriver', 'ActionHelpers']: Self instance """ # XCUITest x W3C spec has no duration by default in server side if self.w3c and duration is None: duration = 600 action = TouchAction(self) if duration is None: action.press(origin_el).move_to(destination_el).release().perform() else: action.press(origin_el).wait(duration).move_to(destination_el).release().perform() return self
參數(shù):
- origin_el - 要滾動(dòng)的起始元素
- destination_el - 要滾動(dòng)到的結(jié)束元素
- duration - 持續(xù)時(shí)間,單位毫秒,默認(rèn)為 600 ms
操作場(chǎng)景
- 進(jìn)入網(wǎng)易云首頁(yè)
- 從課堂直播滑動(dòng)到直播公開(kāi)課位置
關(guān)鍵代碼實(shí)現(xiàn)
# 定位到課堂直播元素 el1 = driver.find_element(AppiumBy.XPATH, "http://*[@text='課堂直播']").click() # 定位到直播公開(kāi)課元素 el2 = driver.find_element(AppiumBy.XPATH, "http://*[@text='直播公開(kāi)課']").click() # 執(zhí)?滑動(dòng)操作 driver.scroll(el1,el2)
說(shuō)明
操作過(guò)程有 慣性,需要添加duration參數(shù),參數(shù)值越大,慣性越小。
drag_and_drop 介紹
說(shuō)明
從一個(gè)元素滑動(dòng)到另一個(gè)元素,第二個(gè)元素代替第一個(gè)元素原本屏幕上的位置。
方法詳情
def drag_and_drop(self: T, origin_el: WebElement, destination_el: WebElement) -> T: """Drag the origin element to the destination element Args: origin_el: the element to drag destination_el: the element to drag to Returns: Union['WebDriver', 'ActionHelpers']: Self instance """ action = TouchAction(self) action.long_press(origin_el).move_to(destination_el).release().perform() return self
參數(shù):
- origin_el - 要滑動(dòng)頁(yè)面的起始元素
- destination_el - 要滑動(dòng)頁(yè)面到結(jié)束元素
操作場(chǎng)景
- 進(jìn)入網(wǎng)易云首頁(yè)
- 從課堂直播滑動(dòng)到直播公開(kāi)課位置
關(guān)鍵代碼實(shí)現(xiàn)
# 定位到課堂直播元素 el1 = driver.find_element(AppiumBy.XPATH, "http://*[@text='課堂直播']").click() # 定位到直播公開(kāi)課元素 el2 = driver.find_element(AppiumBy.XPATH, "http://*[@text='直播公開(kāi)課']").click() # 執(zhí)?滑動(dòng)操作 driver.drag_and_drop(el1,el2)
說(shuō)明
不能設(shè)置持續(xù)時(shí)間,沒(méi)有慣性
滑動(dòng)和拖拽使用場(chǎng)景選擇
滑動(dòng)和拖拽無(wú)非就是考慮是否具有“慣性”,以及傳遞的參數(shù)是“元素”還是“坐標(biāo)”。
- scroll:有 “慣性” ,傳入 “元素”,可以通過(guò)設(shè)置duration參數(shù)來(lái)進(jìn)行控制慣性大小
- drag_and_drop:無(wú) “慣性” ,傳入 “元素”
- swipe:有 “慣性” ,傳入 “坐標(biāo)”,可以通過(guò)設(shè)置duration參數(shù)來(lái)進(jìn)行控制慣性大小
說(shuō)明: 添加duration參數(shù),參數(shù)值越大,慣性越小
到此這篇關(guān)于Appium中scroll和drag_and_drop根據(jù)元素位置滑動(dòng)的文章就介紹到這了,更多相關(guān)Appium 元素滑動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解appium+python 啟動(dòng)一個(gè)app步驟
- Python+Appium實(shí)現(xiàn)自動(dòng)化測(cè)試的使用步驟
- Appium+Python自動(dòng)化測(cè)試之運(yùn)行App程序示例
- Appium Python自動(dòng)化測(cè)試之環(huán)境搭建的步驟
- 詳解Appium+Python之生成html測(cè)試報(bào)告
- 詳解基于Android的Appium+Python自動(dòng)化腳本編寫(xiě)
- Python+Appium新手教程
- python利用appium實(shí)現(xiàn)手機(jī)APP自動(dòng)化的示例
相關(guān)文章
Python操作MySQL數(shù)據(jù)庫(kù)的方法
pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同。接下來(lái)通過(guò)本文給大家介紹Python操作MySQL數(shù)據(jù)庫(kù)的方法,感興趣的朋友一起看看吧2018-06-06利用python模擬實(shí)現(xiàn)POST請(qǐng)求提交圖片的方法
最近在利用python做接口測(cè)試,其中有個(gè)上傳圖片的接口,在網(wǎng)上各種搜索,各種嘗試。下面這篇文章主要給大家介紹了關(guān)于利用python模擬實(shí)現(xiàn)POST請(qǐng)求提交圖片的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07pytorch部署到j(luò)upyter中的問(wèn)題及解決方案
這篇文章主要介紹了pytorch部署到j(luò)upyter中,在這里需要注意我再輸入的時(shí)候出現(xiàn)了一些無(wú)法定位的提示,但是我的電腦沒(méi)有影響使用jupyter,還是可以使用jupyter并且可以import torch,本文給大家講解的非常詳細(xì),需要的朋友參考下吧2022-05-05詳解Python的hasattr() getattr() setattr() 函數(shù)使用方法
這篇文章主要介紹了詳解Python的hasattr() getattr() setattr() 函數(shù)使用方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07