scroll()方法是滑動頁面,不過不是滑動滾動條,而是獲取兩個元素,然后從從一個元素滾動到另一個元素。
方法介紹:
scroll(self, origin_el, destination_el, duration=None):
參數:
- originalEl - 要滾動的元素
- destinationEl - 要滾動到的元素
- dufrom appium import webdriver
從一個元素滾動到另一個元素
def find_element(self, *loc): """ 重寫find_element方法,顯式等待 """ try: # self.driver.wait_activity(self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("%s")'), 10) # WebDriverWait(self.driver, 15).until(EC.visibility_of_element_located(loc)) time.sleep(1) return self.driver.find_element(*loc) except NoSuchElementException as msg: print(u"查找元素異常: %s" % msg) # self.driver.back() # raise msg # 拋出異常 return False
def scroll_page(self, loc1, loc2): stop_element = self.find_element(*loc2) start_element = self.find_element(*loc1) self.driver.scroll(start_element, stop_element, 3000)
--------------------------------------------------
swipe()方法:
滑動屏幕
上下滑動
def swipe_down_up(self, start_y=0.25, stop_y=0.75, duration=2000): # 如果start_y=0.75, stop_y=0.25,則向上滑動屏幕 # 按下手機屏幕,向下滑動 # 注意,向下滑時,x軸不變,要不然就變成了斜向下滑動了 # @duration:持續時間 x = self.driver.get_window_size()["width"] # 獲取屏幕的寬 y = self.driver.get_window_size()["height"] x1 = int(x * 0.5) y1 = int(y * start_y) x2 = int(x * 0.5) y2 = int(y * stop_y) self.driver.swipe(x1, y1, x2, y2, duration)
左右滑動
def swipe_left_right(self, start_x=0.75, stop_x=0.25, duration=2000): # 如果start_x=0.25, stop_x=0.75,屏幕往右滑動 # 按下手機屏幕,向左滑動 # 注意,向上滑時,x軸不變,要不然就變成了斜向上滑動了 # @duration:持續時間 x = self.driver.get_window_size()["width"] # 獲取屏幕的寬 y = self.driver.get_window_size()["height"] x1 = int(x * start_x) y1 = int(y * 0.5) x2 = int(x * stop_x) y2 = int(y * 0.5) self.driver.swipe(x1, y1, x2, y2, duration)
滑動元素:
def swipe_element_du(self, *loc, start_y=0.25, stop_y=0.75, duration=2000): # 默認元素向下滑。如果start_y=0.75, stop_y=0.25,則向上滑動元素 # 獲取元素坐標 x = self.x_y_w_h(*loc)['x'] y = self.x_y_w_h(*loc)['y'] # 獲取元素寬、高 width = self.x_y_w_h(*loc)['w'] height = self.x_y_w_h(*loc)['h'] x1 = int(x + width * 0.5) y1 = int(y + height * start_y) x2 = int(x + width * 0.5) y2 = int(y + height * stop_y) self.driver.swipe(x1, y1, x2, y2, duration)
x_y_w_h()為自己寫的獲取元素的坐標、寬、高:
def x_y_w_h(self, *loc): # 獲取元素坐標,寬,高 # 獲取元素坐標 ele = self.find_element(*loc).location ele_size = self.find_element(*loc).size list_x = { "x": ele.get('x'), "y": ele.get('y'), "w": ele_size['width'], "h": ele_size['height'] } return list_x
scroll() 與swipe()的區別,swipe是可以根據自己需要設置滑動的距離,而scroll是根據頁面中兩個元素位置距離進行滑動。