版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
# -*- encoding: utf-8 -*-
import time
class Waiter:
BY_ID = "BY_ID"
BY_XPATH = "BY_XPATH"
@classmethod
def find_element_by_waiter(cls, driver, locator, max_wait=1, interval=0.5):
max_retry_times = int(float(max_wait) / float(interval))
exception_type = ""
for retry_count in range(max_retry_times):
try:
if locator[0] == Waiter.BY_ID:
element = driver.find_element_by_id(locator[1])
elif locator[0] == Waiter.BY_XPATH:
element = driver.find_element_by_xpath(locator[1])
return element
except Exception as e:
exception_type = e
print("Catch Exception: %s, sleep %s s and try again" % (e, interval))
retry_count += 1
time.sleep(interval)
raise Exception(exception_type)
# -*- encoding: utf-8 -*-
import time
def wait_for(func):
max_wait = 1
interval = 0.5
max_retry_times = int(float(max_wait)/float(interval))
exception_type = ""
for retry_count in range(max_retry_times):
try:
func()
except Exception as e:
exception_type = e
print("Catch Exception: %s, sleep %s s and try again" % (e, interval))
retry_count += 1
time.sleep(interval)
raise Exception(exception_type)
@wait_for
def some_element():
return 1/0
some_element(1111)
# -*- encoding: utf-8 -*-
import time
def wait_for(max_wait=1, interval=0.5):
max_retry_times = int(float(max_wait)/float(interval))
exception_type = ""
for retry_count in range(max_retry_times):
try:
if not retry_count == 5:
d = 1 / 0
else:
d = 1 / 1
except Exception as e:
exception_type = e
print("Catch Exception: %s, sleep %s s and try again" % (e, interval))
retry_count += 1
time.sleep(interval)
raise Exception(exception_type)
wait_for()