自动化过程中有的页面元素加载慢或者需要等待特定条件执行后续步骤,此时需添加等待时间:

1 time.sleep()  固定等待时间,需import time

2 webdriver隐式等待 无需引入包

driver.implicitly_wait(10) 最多等待10s,10s内监测到元素则继续执行,超过10s抛出超时异常。driver生命周期内都有效

3 webdriver显示等待,需引包from selenium.webdriver.support.ui import WebDriverWait

WebDriverWait(driver, 10).until()或until_not() 10s内每500ms监测一次条件是否成立,超过10s超时

下面尝试未设置时间,设置固定时间,设置隐式等待和显示等待四种方式:

1 未设置等待时间,抛出异常未找到元素

#coding:utf-8
#等待时间
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import
dr = webdriver.Firefox()
dr.get("https://www.baidu.com")
dr.find_element_by_id("kw").send_keys("selenium",Keys.ENTER)
#未等待
dr.find_element_by_partial_link_text("Web Browser Automation").click()
dr.quit()

2 设置固定等待时间2s

#coding:utf-8
#等待时间
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
dr = webdriver.Firefox()
dr.get("https://www.baidu.com")
dr.find_element_by_id("kw").send_keys("selenium",Keys.ENTER)
#固定等待时间
time.sleep(2)
dr.find_element_by_partial_link_text("Web Browser Automation").click()
dr.quit()

3 隐式等待

#coding:utf-8
#等待时间
from selenium import webdriver
from selenium.webdriver.common.keys import Keys dr = webdriver.Firefox()
dr.implicitly_wait(10) #设置页面元素的隐式等待时间10s
dr.get("https://www.baidu.com")
dr.find_element_by_id("kw").send_keys("selenium",Keys.ENTER)
dr.find_element_by_partial_link_text("Web Browser Automation").click()
dr.quit()

4 显示等待

#coding:utf-8
#等待时间
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
dr = webdriver.Firefox()
dr.get("https://www.baidu.com")
dr.find_element_by_id("kw").send_keys("selenium",Keys.ENTER)
#显示等待
element = WebDriverWait(dr,10).until(
EC.presence_of_element_located((By.PARTIAL_LINK_TEXT,'Web Browser Automation'))
)
element.click()
dr.quit()

webdriver提供了很多预期条件:

the end!

Selenium入门20 等待时间的更多相关文章

  1. 自动化测试Java一:Selenium入门

    From: https://blog.csdn.net/u013258415/article/details/77750214 Selenium入门 欢迎阅读Selenium入门讲义,本讲义将会重点介 ...

  2. Selenium自动化测试Python一:Selenium入门

    Selenium入门 欢迎阅读Selenium入门讲义,本讲义将会重点介绍Selenium的入门知识以及Selenium的前置知识. 自动化测试的基础 在Selenium的课程以前,我们先回顾一下软件 ...

  3. Selenium 入门到精通系列:六

    Selenium 入门到精通系列 PS:Checkbox方法 例子 HTML: <html> <head> <title>测试页面</title> &l ...

  4. Selenium 入门到精通系列:五

    Selenium 入门到精通系列 PS:显式等待.隐式等待.强制等待方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...

  5. Selenium 入门到精通系列:四

    Selenium 入门到精通系列 PS:鼠标右键.鼠标悬停.键盘操作方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...

  6. Selenium 入门到精通系列:三

    Selenium 入门到精通系列 PS:Driver_Element 常用方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2 ...

  7. Selenium 入门到精通系列:二

    Selenium 入门到精通系列 PS:用户登录 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12 ...

  8. Selenium 入门到精通系列:一

    Selenium 入门到精通系列 PS:控制浏览器窗口大小.前进.后退.刷新 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 20 ...

  9. Selenium入门6 操作元素,获取元素属性

    取元素的text,属性get_attribute,标签名tag_name 操作元素:send_keys输入,click点击,submit提交,clear清除输入 状态判断:is_display,is_ ...

随机推荐

  1. newCachedThreadPool无上限线程池使用

    1. newCachedThreadPool无上限线程池,   动态根据代码添加线程,   如果线程空闲60秒没有被使用,会自动关闭 package ThreadTest; import java.u ...

  2. XtraFinder

    About System Integrity Protection in OS X 10.11 Apple's article . System Integrity Protection blocks ...

  3. 自动截取sql并合并,生成执行HQL

    ### 提取SQL语句 FILE_PATH="/data/SCRIPT" cat tables | while read file do echo "-----> ...

  4. Filter责任链模式

    Filter责任链的创建 org.apache.catalina.core.ApplicationFilterFactory#createFilterChain,  此方法是被org.apache.c ...

  5. Python的Profilers性能分析器

    关于Python Profilers性能分析器 关于性能分析,python有专门的文档,可查看:http://docs.python.org/library/profile.html?highligh ...

  6. 锁、CAS操作和无锁队列的实现

    https://blog.csdn.net/yishizuofei/article/details/78353722 锁的机制 锁和人很像,有的人乐观,总会想到好的一方面,所以只要越努力,就会越幸运: ...

  7. github 0 学习

    Github 快速上手实战教程 一.实验介绍 1.1 实验内容 本次课程讲的是在实验楼的在线环境中,如何使用 Github 去管理在在线环境中使用的代码.配置.资源等实验相关文件,怎样去添加.同步和下 ...

  8. JavaSE---对象序列化

    1.对象序列化机制 允许把内存中的Java对象转换成平台无关的二进制流,从而可以将二进制流持久保存到磁盘 或 在网络中直接传输: (目的:使得对象可以脱离程序的运行而独立存在) package com ...

  9. CTeX里面CTRL-Space和中文输入法的冲突问题解决

    我使用的是windows xp,相信下面的方法也能应用到win7等windows系统上. 我希望在CTex套件的WinEdt 6.0里使用模板自动插入内容时,想快速从上到下遍历” * “并修改. 通过 ...

  10. Hadoop Intro - Configure 01

    hadoop配置文件详解.安装及相关操作   一.      Hadoop伪分布配置 1. 在conf/hadoop-env.sh文件中增加:export JAVA_HOME=/home/Java/j ...