Selenium入门20 等待时间
自动化过程中有的页面元素加载慢或者需要等待特定条件执行后续步骤,此时需添加等待时间:
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 等待时间的更多相关文章
- 自动化测试Java一:Selenium入门
From: https://blog.csdn.net/u013258415/article/details/77750214 Selenium入门 欢迎阅读Selenium入门讲义,本讲义将会重点介 ...
- Selenium自动化测试Python一:Selenium入门
Selenium入门 欢迎阅读Selenium入门讲义,本讲义将会重点介绍Selenium的入门知识以及Selenium的前置知识. 自动化测试的基础 在Selenium的课程以前,我们先回顾一下软件 ...
- Selenium 入门到精通系列:六
Selenium 入门到精通系列 PS:Checkbox方法 例子 HTML: <html> <head> <title>测试页面</title> &l ...
- Selenium 入门到精通系列:五
Selenium 入门到精通系列 PS:显式等待.隐式等待.强制等待方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...
- Selenium 入门到精通系列:四
Selenium 入门到精通系列 PS:鼠标右键.鼠标悬停.键盘操作方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019 ...
- Selenium 入门到精通系列:三
Selenium 入门到精通系列 PS:Driver_Element 常用方法 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2 ...
- Selenium 入门到精通系列:二
Selenium 入门到精通系列 PS:用户登录 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12 ...
- Selenium 入门到精通系列:一
Selenium 入门到精通系列 PS:控制浏览器窗口大小.前进.后退.刷新 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 20 ...
- Selenium入门6 操作元素,获取元素属性
取元素的text,属性get_attribute,标签名tag_name 操作元素:send_keys输入,click点击,submit提交,clear清除输入 状态判断:is_display,is_ ...
随机推荐
- LeeCode(No2 - Add Two Numbers)
LeeCode是一个有意思的编程网站,主要考察程序员的算法 第二题: You are given two non-empty linked lists representing two non-neg ...
- 协议 + socket import 和 form xx import *的区别 028
一 . 网络通信协议(了解) 1 . osi 七层协议 (最好记住 面试会问) 应表会传网数物(应用层 表示层 会话层 传输层 网络层 数据链路层 物理层) 2 .tcp/ip五层 或 tcp/ip四 ...
- iPhone10.3.X越狱后SSH连接不上的问题(已解决)
iPhone10.3.X越狱后SSH连接不上的问题 G0blin RC2,iPhone5s10.3.3 Jailbreak 最近研究了好几天,试了好多的方法. ssh 访问越狱iPhone的两种方式 ...
- opencv-将分离合并图像(Red通道>125置255<=置0)
#include <iostream> #include <opencv2/opencv.hpp> #include "opencv2/imgproc/imgproc ...
- Vue.js-----轻量高效的MVVM框架(七、表单控件绑定)
话不多说,先上完整代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- Selenium+Python+Webdriver:保存截图到指定文件夹
保存图片到指定文件夹: from selenium import webdriverfrom pathlib import Pathfrom time import sleepdriver = web ...
- =与==、&与&&、| 与 || 的区别
=与== =属于赋值运算符,将右侧的值赋给左侧的变量名称 ==属于关系运算符,判断左右两边值是否相等,结果为boolean类型 &与&& &是逻辑与,&& ...
- SSM项目spring配置文件详细步骤(分门别类、灵巧记忆)
spring-dao.xml文件 1.配置外部db.property文件: <context:property-placeholder location="classpath:jdbc ...
- 执行ORACLE SQL时如何 忽略替换变量(转载)
你想在SQL*Plus里执行一个脚本,脚本里包含了一些看起来像替换变量的元素,但实际上你并不是想把它们当替换变量来处理.这时你想让解析器忽略它们而不是提示用户输入.解决方案1有一种解决方案就是在&am ...
- org.apache.subversion.javahl.ClientException: Working copy is not up-to-date
之前因为将项目中的一个文件删除了,然后添加了新的文件,svn提交的时候报错. 提示:Working copy is not up-to-date 解决办法:对应的项目上右键然后选择team,然后选择u ...