美文网首页
Selenium的三种等待方式

Selenium的三种等待方式

作者: 吱吱菌啦啦 | 来源:发表于2022-04-27 18:23 被阅读0次

    直接等待

    • time.sleep(1),强制等待线程休眠一定时间
    • 使用上简单粗暴
    以灵题库网站账号密码登录(http://www.lingtiku.com/#/)为例~
    from time import sleep
    from selenium import webdriver
    
    class TestLingtiku:
        """
           启动ChromeDriver,测试灵题库账号密码登录
           :return:
           """
        def setup(self):
            self.driver = webdriver.Chrome()
            self.driver.get("http://www.lingtiku.com/#/")
            sleep(1)
    
        def teardown(self):
            self.driver.quit()
    
        def test_case(self):
            self.driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div/div/div/a').click()
            sleep(1)
            self.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/div/div[1]/input').send_keys('你的账号')
            sleep(1)
            self.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/div/div[2]/span/input').send_keys('你的密码')
            sleep(1)
            self.driver.find_element_by_xpath('// *[ @ id = "root"] / div / div / div[2] / div / div[4]').click()
    

    隐式等待

    • self.driver.implicitly_wait(5)
    • 设置一个等待时间,轮询查找元素是否出现,如果没出现就抛异常,默认0.5s
    • 全局的,适用于所有的find_element()方法
    • 缺点:用例加载时间有的很长,有的很短,但是我们全局设置的时间是写死的,不能很好地适配每一条用例
    from selenium import webdriver
    class TestLingtiku:
        """
           启动ChromeDriver,测试灵题库账号密码登录
           :return:
           """
        def setup(self):
            self.driver = webdriver.Chrome()
            # 隐式等待
            self.driver.implicitly_wait(1)
            self.driver.get("http://www.lingtiku.com/#/")
    
        def teardown(self):
            self.driver.quit()
    
        def test_case(self):
            self.driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div/div/div/a').click()
            self.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/div/div[1]/input').send_keys('你的账号')
            self.driver.find_element_by_xpath('//*[@id="root"]/div/div/div[2]/div/div[2]/span/input').send_keys('你的密码')
            self.driver.find_element_by_xpath('// *[ @ id = "root"] / div / div / div[2] / div / div[4]').click()
    

    显示等待

    • 在代码中定义等待条件,当条件发生时才继续执行代码;
    • WebDriverWait配合until()和until_not()方法,根据判断条件进行等待;
    • 程序每隔一段时间进行条件判断(时间默认0.5s)如果条件成立,则执行下一步,否则继续等待,直到超过设置的最长时间;
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait
    
    
    class TestLingtiku:
        """
        测试灵题库账号密码登录功能
    
        :return:
        """
        def setup(self):
            self.driver = webdriver.Chrome()
            # 隐式等待
            self.driver.implicitly_wait(3)
            self.driver.get("http://www.lingtiku.com/#/")
    
        def teardown(self):
            self.driver.quit()
    
        def test_case(self):
            self.driver.find_element(By.XPATH, '//*[@id="root"]/div/div[1]/div/div/div/a').click()
            self.driver.find_element(By.XPATH, '//*[@id="root"]/div/div/div[2]/div/div[1]/input').send_keys('你的账号')
            self.driver.find_element(By.XPATH, '//*[@id="root"]/div/div/div[2]/div/div[2]/span/input').send_keys('你的密码')
            # 判断登录按钮是否可点击
            WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, '// *[ @ id = "root"] / div / div / div[2] / div / div[4]')))
            self.driver.find_element(By.XPATH, '// *[ @ id = "root"] / div / div / div[2] / div / div[4]').click()
    

    相关文章

      网友评论

          本文标题:Selenium的三种等待方式

          本文链接:https://www.haomeiwen.com/subject/qtqcyrtx.html