【问题描述】
封装滚动方法swipe(self,num,by,locator),其中by为元素定位方法,测试方法self.swipe_find(3, MobileBy.XPATH, ‘//*[@text=“添加成员”]’).click(),想要传递的参数为MobileBy.XPATH,实际debug观察到传递的参数by是xpath(如下图所示),导致查找不到元素,
请老师和同学们帮忙查看一下问题,谢谢;
swipe_find定位方法代码如下:
def swipe_find(self,num,by,locator):
"""
滚动查找元素
:param num: 滚动次数
:param by: 定位方式
:param locator: 定位元素
:return:
"""
for i in range(num):
try:
element = self.driver.find_element(MobileBy.XPATH, locator)
return element
except:
# 获取页面尺寸
size = self.driver.get_window_size()
height = size.get("height")
width = size.get("width")
# 页面滚动起始位置、终点位置和移动的时间
start_x = width / 2
start_y = 0.8 * height
end_x = width / 2
end_y = 0.4 * height
duration = 2000
# 滚动页面
self.driver.swipe(start_x, start_y, end_x, end_y, duration)
if i == num - 1:
# 未找到元素,抛出异常,并输出滚动的次数
raise NoSuchElementException(f'滚动了{num}次,未找到元素{element}')