自动化测试 智能等待 解决元素操作不稳定问题
# 判断元素是否存在 def isElementExist(driver, element_xpath): flag = True try: driver.find_element_by_xpath(element_xpath) print("Good~找到了元素:"+element_xpath) return flag except: flag = False print("≧ ﹏ ≦未找到元素:" + element_xpath) return flag # 智能等待
def smart_wait(driver, element_xpath):
# 循环18次 每次等1秒
for i in range(18):
if i >= 17:
print("smart_wait time out" + str(i))
break
else:
try:
# 如果找到元素就返回元素,然后break
if isElementExist(driver, element_xpath) is True:
element = driver.find_element_by_xpath(element_xpath)
return element
except:
print("wait for find element")
sleep(1)
return element