一.隐式等待
隐式等待是通过一定的时长等待页面上某元素加载完成。如果超出了设置的时长元素还没有加载完成,则会抛出NoSuchElementException的异常。
一旦设置,隐式等待时间就会作用于这个webdriver实例的整个周期或者一次完整测试的执行周期,并且webdriver会使其对所有测试步骤中包含整个界面的元素的查找时都有效,除非把默认时间设置回0。
webdriver提供了implicitly_wait()方法来实现隐式等待,默认参数是以秒为单位。如下实例:
driver.implicitly_wait(10)
二.显式等待
WebDriverWait(driver,timeout=10,poll_frequency=0.5,ignored_exceptions=None)
- driver:webdriver实例
- timeout:最长超时时间,默认以秒为单位
- poll_frequency:检测的时间间隔,默认为0.5秒
- ignored_exceptions:超时后的异常信息,默认情况下抛出TimeoutException异常
from appium.webdriver import Remote
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
desired_cap = {
"platformName": "Android",
"platformVersion": "7.1.2",
"deviceName": "HUAWEI",
"appPackage": "com.xxxxx",
"appActivity": "com.xxxxxx.activity.WelcomeActivity"
}
driver = Remote(command_executor='http://127.0.0.1:4723/wd/hub',
desired_capabilities=desired_cap)
driver.implicitly_wait(10)
locator = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("我的班级")')
WebDriverWait(driver, 10, 0.5).until(EC.visibility_of_element_located(locator))
三.等待等待
import time
time.sleep(5)