WebDriver提供了两种类型的等待:显示等待和隐式等待
1.显示等待:等待某个条件成立时继续执行,否则在达到最大时长时抛出超时异常
import unittest
import os
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
PATH=lambda p:os.path.abspath(os.path.join(os.path.dirname(__file__),p))
class LoginTests(unittest.TestCase):
def setUp(self):
desired_caps={
'platformName':'Android',
'platformVersion':'6.0.1',
'deviceName':'0715f739044f0d3a',
'app':PATH('D:\kuyu.apk'),
'unicodeKeyboard':True, #使用unicodeKeyboard的编码方式来发送字符串
'resetKeyboard':True#将键盘给隐藏起来
}
self.driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)#初始化
#登录
def test_login(self):
WebDriverWait(self.driver,20).until(lambda the_driver: the_driver.find_element_by_id("com.kuyu:id/tv_login").is_displayed())
self.driver.find_element_by_id("com.kuyu:id/tv_login").click()
self.driver.find_element_by_id("com.kuyu:id/et_email").send_keys("wxy001@qq.com")
self.driver.find_element_by_id("com.kuyu:id/et_pwd").send_keys("123456")
self.driver.find_element_by_id("com.kuyu:id/tv_login").click()
WebDriverWait(self.driver,20).until(lambda the_driver: the_driver.find_element_by_id("com.kuyu:id/include_study_iv_add").is_displayed())
def tearDown(self):
#print("hello")
self.driver.close_app()
self.driver.quit()
if __name__ == '__main__':
unittest.main()
2.隐式等待
#设置默认等待为10秒
driver.implicitly_wait(10)
import time
import unittest
import os
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
PATH=lambda p:os.path.abspath(os.path.join(os.path.dirname(__file__),p))
class LoginTests(unittest.TestCase):
def setUp(self):
desired_caps={
'platformName':'Android',
'platformVersion':'6.0.1',
'deviceName':'0715f739044f0d3a',
'app':PATH('D:\kuyu.apk'),
'unicodeKeyboard':True, #使用unicodeKeyboard的编码方式来发送字符串
'resetKeyboard':True#将键盘给隐藏起来
}
self.driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)#初始化
self.driver.implicitly_wait(10)
#登录
def test_login(self):
self.driver.find_element_by_id("com.kuyu:id/tv_login").click()
self.driver.find_element_by_id("com.kuyu:id/et_email").send_keys("wxy001@qq.com")
self.driver.find_element_by_id("com.kuyu:id/et_pwd").send_keys("123456")
self.driver.find_element_by_id("com.kuyu:id/tv_login").click()
try:
print(time.ctime())
self.driver.find_element_by_id("com.kuyu:id/include_study_iv_ad").click()
except NoSuchElementException as e:
print(e)
finally:
print(time.ctime())
def tearDown(self):
#print("hello")
self.driver.close_app()
self.driver.quit()
if __name__ == '__main__':
unittest.main()
这个元素id 输入错误的值,通过打印的两个时间,可以看出,超过了10秒的等待self.driver.find_element_by_id("com.kuyu:id/include_study_iv_ad").click()
运行结果如下图:
3.sleep休眠方法:执行到某一个位置的时候做固定时间的休眠
sleep()方法是由python的time模块提供的
import time
import unittest
import os
from appium import webdriver
PATH=lambda p:os.path.abspath(os.path.join(os.path.dirname(__file__),p))
class LoginTests(unittest.TestCase):
def setUp(self):
desired_caps={
'platformName':'Android',
'platformVersion':'6.0.1',
'deviceName':'0715f739044f0d3a',
'app':PATH('D:\kuyu.apk'),
'unicodeKeyboard':True, #使用unicodeKeyboard的编码方式来发送字符串
'resetKeyboard':True#将键盘给隐藏起来
}
self.driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)#初始化
#登录
def test_login(self):
self.driver.find_element_by_id("com.kuyu:id/tv_login").click()
self.driver.find_element_by_id("com.kuyu:id/et_email").send_keys("wxy001@qq.com")
self.driver.find_element_by_id("com.kuyu:id/et_pwd").send_keys("123456")
self.driver.find_element_by_id("com.kuyu:id/tv_login").click()
time.sleep(10)
self.driver.find_element_by_id("com.kuyu:id/include_study_iv_ad").click()
def tearDown(self):
#print("hello")
self.driver.close_app()
self.driver.quit()
if __name__ == '__main__':
unittest.main()