1.while 循环
while True:
try:
if driver.find_element_by_xpath("xxx"):
break
else:
driver.driver.swipe(sx, sy, sx, ey)
2.递归
def fine_ele(driver):
if driver.find_element_by_xpath("xxx")
return True
else:
driver.swipe(sx, sy, sx, ey)
fine_ele(driver)
上面这样写会报,元素不存在
修改如下:
def swipe_up(self):
s = self.driver.get_window_size()
x1 = s['width'] * 0.5 # x坐标
y1 = s['height'] * 0.75 # 起点y坐标
y2 = s['height'] * 0.25 # 终点y坐标
print('手机的尺寸是: ', s)
self.driver.swipe(x1, y1, x1, y2, 500)
def find_element_up(self, locate_element):
_source_list = self.driver.page_source
#print(_source_list)
if locate_element in _source_list:
return True
else:
self.swipe_up()
self.find_element_up(locate_element)
调用函数如下:
def scroll_game_book_to_head(self):
'''将新游预约游戏滑动到测试游戏模块'''
_locate_element = '新游预约'
self.find_element_up(_locate_element)