WebDriver设置元素等待

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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值