博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5. Waits
阅读量:5998 次
发布时间:2019-06-20

本文共 3587 字,大约阅读时间需要 11 分钟。

These days most of the web apps are using AJAX techniques. When a page is loaded to browser, the elements within that page may load at different time intervals. This makes locating elements difficult, if the element is not present in the DOM, it will raise ElementNotVisibleException exception. Using waits, we can solve this issue. Waiting provides some time interval between actions performed - mostly locating element or any other operation with the element.

Selenium Webdriver provides two types of waits - implicit & explicit. An explicit wait makes WebDriver to wait for a certain condition to occur before proceeding further with executions. An implicit wait makes WebDriver to poll the DOM for a certain amount of time when trying to locate an element.

5.1. Explicit Waits

An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code. The worst case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

from selenium import webdriverfrom selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Firefox() driver.get("http://somedomain/url_that_delays_loading") try: element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "myDynamicElement")) ) finally: driver.quit()

This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

Expected Conditions

There are some common conditions that are frequent when automating web browsers. Listed below are Implementations of each. Selenium Python binding provides some convienence methods so you don’t have to code an expected_condition class yourself or create your own utility package for them.

  • 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 - it is Displayed and Enabled.
  • 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
from selenium.webdriver.support import expected_conditions as EC wait = WebDriverWait(driver, 10) element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))

The expected_conditions module contains a set of predefined conditions to use with WebDriverWait.

5.2. Implicit Waits

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

from selenium import webdriverdriver = webdriver.Firefox() driver.implicitly_wait(10) # seconds driver.get("http://somedomain/url_that_delays_loading") myDynamicElement = driver.find_element_by_id("myDynamicElement")

转载于:https://www.cnblogs.com/kenfang/articles/5757144.html

你可能感兴趣的文章
乱的一逼。。。。
查看>>
我的友情链接
查看>>
linux下搭建FTP服务器
查看>>
windows 2003 远程桌面连接方法
查看>>
通过nova help命令,
查看>>
sed 的相关用法
查看>>
PLSQL集合表类型与对象表区别
查看>>
huffman树实现的压缩算法,java
查看>>
我的友情链接
查看>>
中国容器厂商综合实力排名,新鲜出炉!
查看>>
BZOJ1834[ZJOI2010]网络扩容——最小费用最大流+最大流
查看>>
Maven学习总结(7)——eclipse中使用Maven创建Web项目
查看>>
Java基础学习总结(18)——网络编程
查看>>
Spring常用注解
查看>>
代码托管平台简介
查看>>
Spring MVC常用注解说明
查看>>
Emacs之ido-mode笔记
查看>>
windows server 2012 R2登录密码忘了怎么办呢?
查看>>
Linux入门命令100条
查看>>
SDN 技术指南(一):架构概览
查看>>