为什么需要等待时间:页面加载需要时间,如果页面没有加载完成,直接去定位,可能定位不到元素

1、强制等待:

import time

time.sleep(2) 不管有没有完成加载,必须等待2秒

2、隐式等待:

driver = webdriver.Chrome()
driver.implicity_wait(10)
driver.get('http://www.baidu')
implicity_wait()默认参数的单位为妙,首先这10秒并非一个固定的等待时间,它并不影响脚本的执行速度。其次,它并不针对页面上的某一元素进行等待。当脚本执行到某个元素定位时,如果元素可以定位到,则继续执行;如果元素定位不到,则它将以轮询的方式不断地判断元素是否被定位到。假设在第六秒定位到了元素则继续执行,若直到超出设置的时长10秒还没有定位到元素,则抛出异常。
3、显式等待:
  

 from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver,5,0.5).until(
EC.presence_of_element_located((By.ID,'kw'))
) element1 = WebDriverWait(driver,60).until(EC.element_to_be_clickable((By.ID,'kw')))
element.send_keys('hello')
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)
driver:浏览器驱动
timeout:最长超过时间,默认以秒为单位
poll_frequency:监测的时间间隔,默认为0.5秒
ignored_exceptions:超时后的异常信息,默认情况下抛NoSuchElementException异常
WebDriverWait一般有until和until_not方法配合使用
until(method,message)
until_not(method ,message)
如果成功,则执行下一步,否则继续等待,直到超过设置的最长时间,抛出异常
注意:显式等待与隐式等待可以同时使用,等待时间为两者中最大的
EC下方法很多:
  
 

selenium三个等待方法的更多相关文章

  1. Python selenium 三种等待方法

    1. 强制等待 sleep(xx) 是最简单粗暴的一种办法,不管你浏览器是否加载完了,程序都得等待3秒,3秒一到,继续执行下面的代码,作为调试很有用,不建议总用这种等待方式,严重影响程序执行速度. 代 ...

  2. python之selenium三种等待方法

    前提: 我们在做Web自动化时,有的时候要等待元素加载出来,才能操作,不然会报错 1.强制等待 2.隐式等待 3.显示等待 内容: 一,强制等待 这个比较简单,就是利用time模块的sleep的方法来 ...

  3. selenium webdriver三种等待方法

    webdriver三种等待方法 1.使用WebDriverWait from selenium import webdriverfrom selenium.webdriver.common.by im ...

  4. 【转】selenium webdriver三种等待方法

    原文:https://www.cnblogs.com/lgh344902118/p/6015593.html webdriver三种等待方法 1.使用WebDriverWait from seleni ...

  5. Python+Selenium自动化-设置等待三种等待方法

    Python+Selenium自动化-设置等待三种等待方法   如果遇到使用ajax加载的网页,页面元素可能不是同时加载出来的,这个时候,就需要我们通过设置一个等待条件,等待页面元素加载完成,避免出现 ...

  6. python selenium 三种等待方式详解[转]

    python selenium 三种等待方式详解   引言: 当你觉得你的定位没有问题,但是却直接报了元素不可见,那你就可以考虑是不是因为程序运行太快或者页面加载太慢造成了元素不可见,那就必须要加等待 ...

  7. 深入selenium三种等待方式使用

    深入selenium三种等待方式使用 处理由于网络延迟造成没法找到网页元素 方法一 用time模块不推荐使用 用time模块中的time.sleep来完成等待 from selenium import ...

  8. Selenium 三种等待

    问题 : 强制等待和隐式等待的区别怎么理解? 和pause有什么区别?什么时候适用pause? 第二篇文章更清楚一点. 以下内容引自: https://www.cnblogs.com/xu-jia-l ...

  9. Selenium三种等待元素的方式及代码,需要特别注意implicitlyWait的用法

    一.显式等待 1.显式等待: 就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception. 2.代码: new WebDr ...

随机推荐

  1. docker build提示error checking context:can't stat xxx

    现象描述 使用docker build一个镜像的时候,提示下面的错误: ➜ docker build -t image_name -f xxx.dockerfile . error checking ...

  2. lamda表达式求最小值

    //取最小设置金额的列 var minList = LsList.OrderBy(n=>n.Price).First(); //取最小设置金额的值 var minValue = LsList.S ...

  3. java html实体转义

    public static void main(String[] args) { String str = " -<->-&-"-&apos;" ...

  4. Vue 组件封装发布到npm 报错 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

    Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 原因是 没有导出 export default { name:& ...

  5. Content-Type四种常见取值

    一.application/x-www-form-urlencoded 最常见 POST 提交数据的方式.浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 applica ...

  6. [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  7. SimpleDataFormat 线程不安全解决

    SimpleDataFormat 是我们常用的时间转换工具,我再spark中使用sdf对时间戳进行转换,发现转化出的时间有异常,原来sdf是线程不安全的, 改用joda time,错误消失,样例如下 ...

  8. jquery监听回车

    jquery监听回车 <pre> $("#loginusername, #loginpassword, #code").keydown(function() { if( ...

  9. kafka web端管理工具 kafka-manager【转发】

    1,下载与安装 $ git clone https://github.com/yahoo/kafka-manager.git $ cd kafka-manager $ ./sbt clean dist ...

  10. C/C++ 每日一题

    超长正整数的相加,题目链接:https://www.nowcoder.com/practice/5821836e0ec140c1aa29510fd05f45fc?tpId #include<al ...