UI自动化测试过程中,可能会出现因测试环境不稳定、网络慢等情况,如果不做任何处理的话,会出现无法定位到特定元素而报错,导致自动化测试无法顺利执行。
selenium官网手册:Waits | Selenium
slenium自动化测试中,主要涉及三种等待方式:
1 使用python自带模块time的sleep方式
缺点:即使网络条件较好时,依旧按照预定固定时间等待,一般不建议使用,脚本调试可使用。
示例脚本:
from selenium import webdriver
from time import sleep
class TestWait(object):
driver = webdriver.Chrome()
driver.get(http://www.baidu.com)
def test_sleep(self):
self.driver.find_element_by_id("kw").send_keys("sleep test")
# sleep(2) #等待固定时间
self.driver.implicitly_wait(2) # 隐式等待
self.driver.find_element_by_id("su").click()
self.driver.quit()
if __name__ == '__main__':
wait=TestWait()
wait.test_sleep()
2 隐式等待(implicitly_wait)
隐式等待设置的时间是最长的时间,如果在规定时间内网页加载完成,则执行下一步,否则一直等到时间结束,然后执行下一步。
注意:隐式等待对driver整个周期都起作用,一般在最开始设置一次就可以了。不要当做固定等待,哪里都设置隐式等待。
示例脚本:
from selenium import webdriver
from time import sleep
class TestWait(object):
driver = webdriver.Chrome()
driver.get(http://www.baidu.com)
def test_sleep(self):
self.driver.find_element_by_id("kw").send_keys("sleep test")
self.driver.implicitly_wait(2) # 隐式等待
self.driver.find_element_by_id("su").click()
self.driver.quit()
if __name__ == '__main__':
wait=TestWait()
wait.test_sleep()
3 显示等待(WebDriverWait)
显式等待允许等待条件的发生,所以非常适合在浏览器及其DOM和WebDriver脚本之间同步状态。
需要引入包:from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait参数说明:
WebDriverWait(driver, timeout=3).until(some_condition)
两种方法:until和util_not
场景:
打开百度首页,等待页面出现:百度一下,你就知道,再执行输入搜索关键词,点击“百度一下”按钮。
示例脚本:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
class TestWait(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get(http://www.baidu.com)
def test_webdreiverwait(self):
webdreiverwaits =WebDriverWait(self.driver,2)
webdreiverwaits.until(ec.title_is("百度一下,你就知道"))
self.driver.find_element_by_id("kw").send_keys("test_webdreiverwait test")
self.driver.find_element_by_id("su").click()
def teardown(self):
self.driver.quit()
if __name__ == '__main__':
wait=TestWait()
wait.test_webdreiverwait()
三种等待完整示例脚本:
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
class TestWait(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get("http://www.baidu.com")
def test_sleep(self):
self.driver.find_element_by_id("kw").send_keys("sleep test")
sleep(2) #等待固定时间
self.driver.find_element_by_id("su").click()
def test_implicitly(self):
self.driver.find_element_by_id("kw").send_keys("implicitly test")
self.driver.implicitly_wait(2) # 隐式等待
self.driver.find_element_by_id("su").click()
def test_webdreiverwait(self):
webdreiverwaits =WebDriverWait(self.driver,2)
webdreiverwaits.until(ec.title_is("百度一下,你就知道"))
self.driver.find_element_by_id("kw").send_keys("test_webdreiverwait test")
self.driver.find_element_by_id("su").click()
def teardown(self):
self.driver.quit()
if __name__ == '__main__':
wait=TestWait()
# wait.test_sleep()
# wait.test_implicitly()
wait.test_webdreiverwait()
【常见问题】:运行脚本报empty suite:
from selenium import webdriver
from time import sleep
class TestWait(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('http://www.baidu.com')
def test_sleep(self):
self.driver.find_element_by_id("kw").send_keys("sleep test")
# sleep(2) #等待固定时间
self.driver.implicitly_wait(2) # 隐式等待
self.driver.find_element_by_id("su").click()
self.driver.quit()
if __name__ == '__main__':
wait=TestWait()
wait.test_sleep()
【解决方法】:脚本修改
from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
class TestCase(object):
def setup(self):
self.driver = webdriver.Chrome()
self.driver.get('http://www.baidu.com')
# sleep(2)
def test_sleep(self):
self.driver.find_element_by_id('kw').send_keys('selenium')
# sleep(2) # 线程阻塞 blocking wait
self.driver.find_element_by_id('su').click()
# sleep(3)
def test_implicitly(self):
self.driver.implicitly_wait(10)
self.driver.find_element_by_id('kw').send_keys('selenium')
# sleep(2) # 线程阻塞 blocking wait
self.driver.find_element_by_id('su').click()
# sleep(3)
def test_wait(self):
wait = WebDriverWait(self.driver,2)
wait.until(EC.title_is('百度一下,你就知道'))
self.driver.find_element_by_id('kw').send_keys('selenium')
# sleep(2) # 线程阻塞 blocking wait
self.driver.find_element_by_id('su').click()
# sleep(3)
def teardown(self):
self.driver.quit()
if __name__ == '__main__':
case = TestCase()
# case.test_sleep()
# case.test_implicitly()
case.test_wait()
以上:极客时间课程:selenium自动化测试学习总结!
以上就是python自动化测试selenium核心技术三种等待方式详解的详细内容,更多关于selenium三种等待方式的资料请关注编程网其它相关文章!