问题描述:
自定义的 handle_black 装饰器,导致自定义滚动查找 swipe_find 失效
装饰器导致无法进行到 swipe_find 的异常处理步骤
请问如何改造才能使 swipe_find 生效
详细代码:
find
@handle_black
def find(self, by, locator=None) -> WebElement:
if locator is None:
result = self.driver.find_element(*by)
else:
result = self.driver.find_element(by, locator)
handle_black.py
import functools
import logging
import allure
from appium.webdriver import WebElement
from appium.webdriver.common.mobileby import MobileBy
def handle_black(fun):
def wrapper(*args, **kwargs):
logging.basicConfig(level=logging.INFO)
_black_list = [
(MobileBy.ID, "mIvClose"),
(MobileBy.ID, "tv_agree"),
(MobileBy.ID, "main_permission_btn"),
(MobileBy.ID, "btn_start"),
(MobileBy.ID, "gb_sure"),
(MobileBy.ID, "iv_close")
]
_max_err_num = 3
_error_num = 0
from Oxygen_UI_Android.page.base_page import BasePage
instance: BasePage = args[0]
try:
logging.info(
"run "
+ fun.__name__
+ "\n args:"
+ repr(args[1:])
+ "\n"
+ repr(kwargs)
)
element = fun(*args, **kwargs)
_error_num = 0
instance.set_implicitly_wait(5)
return element
except Exception as e:
instance.screenshot("../pics/pics.png")
with open("../pics/pics.png", "rb") as f:
content = f.read()
allure.attach(content, attachment_type=allure.attachment_type.PNG)
logging.error("元素未找到,开始处理黑名单流程")
instance.set_implicitly_wait(1)
if _error_num > _max_err_num:
_error_num = 0
raise e
_error_num += 1
for ele in _black_list:
eles = instance.finds(ele)
if len(eles) > 0:
eles[0].click()
return wrapper(*args, **kwargs)
raise ValueError("元素不在黑名单中")
return wrapper
swipe_find
def swipe_find(self, locator):
self.driver.implicitly_wait(1)
num = 5
for i in range(0, num):
if i == num - 1:
raise NoSuchElementException(f"找了{i}次,未找到")
try:
element = self.find(locator)
self.driver.implicitly_wait(5)
return element
except NoSuchElementException:
print("未找到,滑动")
size = self.driver.get_window_size()
width = size["width"]
height = size["height"]
start_x = width / 2
start_y = height * 0.8
end_x = start_x
end_y = height * 0.3
duration = 2000
self.driver.swipe(start_x, start_y, end_x, end_y, duration)```