zoukankan      html  css  js  c++  java
  • Selenium with Python 008

        如今大多数Web应用程序使用Ajax技术,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成的,这给元素的定位增加了困难。如果因为在加载某个元素时延迟而造成ElementNotVisibleException的情况出现,那么就会降低自动化脚本的稳定性,我们可以通过设置元素等待改善这种问题造成的不稳定。

        WebDriver提供了两种类型的等待:含蓄等待明确等待。明确等待作用于特定代码块,使得WebDriver等待某个条件成立时继续执行,否则在达到最大时长时抛出超时异常;而含蓄等待,属于全局超时设置,则会让WebDriver在指定的时间内不断轮询DOM尝试定位元素,直到成功定位元素或超时。

    明确等待

    方式一:比较极端的方式是通过time.sleep() 来让程序休眠指定时间,然后继续执行。

    方式二:结合WebDriverWait和ExpectedCondition 来设置等待。WebDriverWait 类是有WebDriver提供的等待方法,在设置时间内,默认每隔一段时间检测一次当前页面元素是否存在。具体格式如:WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)

    #!/usr/bin/env python 
    # -*- coding: utf-8 -*-
    
    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
    import time
    
    driver = webdriver.Chrome()
    driver.get("http://www.baidu.com")
    
    wait = WebDriverWait(driver, 5, 0.5)
    element = wait.until(EC.presence_of_element_located((By.ID, "kw")))
    element.send_keys("selenium")
    time.sleep(5)
    driver.close()

    如上代码块,打开百度首页后,除非能定位到ID为“kw”的控件才继续往下执行,否则将会一直等待到5s后抛出超时异常。WebDriverWait默认每500毫秒去检查一次ExpectedCondition 

    expected_conditions类所提供的预期条件判断的方法如下:

    • title_is
    • title_contains
    • presence_of_element_located
    • visibility_of_element_located
    • visibility_of
    • presence_of_all_elements_located
    • text_to_be_present_in_element
    • text_to_be_present_in_element_value
    • frame_to_be_available_and_switch_to_it
    • invisibility_of_element_located
    • element_to_be_clickable
    • staleness_of
    • element_to_be_selected
    • element_located_to_be_selected
    • element_selection_state_to_be
    • element_located_selection_state_to_be
    • alert_is_present

    含蓄等待

    An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object.

    implicitly_wait 属于全局智能等待时间,一旦设置,作用于整个WebDriver生命周期,它不是固定的等待时间,一旦定位到元素就继续往下执行。

    #!/usr/bin/env python 
    # -*- coding: utf-8 -*-
    
    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.get("http://www.baidu.com")
    
    element = driver.find_element_by_id("kw")
    element.send_keys("selenium")
    driver.close()
    ***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
  • 相关阅读:
    bzoj 4539 [Hnoi2016]树——主席树+倍增
    bzoj 4137 [FJOI2015]火星商店问题——线段树分治+可持久化01trie树
    bzoj 4025 二分图——线段树分治+LCT
    LOJ 121 「离线可过」动态图连通性——LCT维护删除时间最大生成树 / 线段树分治
    bzoj 3572 [Hnoi2014]世界树——虚树
    bzoj 4650(洛谷 1117) [Noi2016]优秀的拆分——枚举长度的关键点+后缀数组
    洛谷 P3957 跳房子 —— 二分答案+单调队列优化DP
    洛谷 P1578 奶牛浴场 —— 最大子矩形
    bzoj 1510 Kra-The Disks —— 思路
    bzoj 1657 Mooo 奶牛的歌声 —— 单调栈
  • 原文地址:https://www.cnblogs.com/guanfuchang/p/7493947.html
Copyright © 2011-2022 走看看