Selenium - set element wait

1. sleep () sleep method -- time fixed waiting

In the process of developing the automation framework, it is most taboo to use the time sleep method of Python's own module to wait. Although the waiting time can be customized, when the network conditions are good,

Continue to wait according to the preset time, resulting in an infinite extension of the automation time of the entire project. Not recommended for use.

Second, implicitly_wait (implicit wait)

Implicit wait is to wait for an element on the page to load for a certain period of time. Throws a NoSuchElementException if the element has not been loaded after the set duration.

Implicitly_wait method to implement implicit, etc. The default setting is 0 

3. WebDriverWait (display waiting)

Display waiting makes WebDriver wait for a certain condition to continue to execute, otherwise it will throw a TimeoutException when the maximum duration is reached.

Source code reference:

import time
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException

POLL_FREQUENCY = 0.5 # How long to sleep inbetween calls to the method
IGNORED_EXCEPTIONS = (NoSuchElementException,) # exceptions ignored during calls to the method


class WebDriverWait(object):
def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
"""Constructor, takes a WebDriver instance and timeout in seconds.

:Args:
- driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
- timeout - Number of seconds before timing out
- poll_frequency - sleep interval between calls
By default, it is 0.5 second.
- ignored_exceptions - iterable structure of exception classes ignored during calls.
By default, it contains NoSuchElementException only.

Example:
from selenium.webdriver.support.ui import WebDriverWait \n
element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
until_not(lambda x: x.find_element_by_id("someId").is_displayed())
"""
self._driver = driver
self._timeout = timeout
self._poll = poll_frequency

driver :浏览器驱动
timeout :最长超时时间,默认以秒为单位
poll_frequency :检测的间隔(步长)时间,默认为0.5S
ignored_exceptions :超时后的异常信息,默认情况下抛出 NoSuchElementException异常
WebDriverWait()  一般有until() 或 until_not() 方法配合使用
引入方法:
from selenium.webdriver.support.wait import  WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec

driver.find_element_by_css_selector('xx')
# WebDriverWait(driver,10).until(Ec.title_is('xx')) #等什么出现则继续执行
# WebDriverWait(driver,10).until_not() #等什么消失则继续执行,不常用
# e = WebDriverWait(driver,10).until(Ec.presence_of_element_located(('id','i1')))#等什么出现则继续执行
expected_conditions 类提供的预期条件判断方法如下:
1 title_is: 判断当前页面的title是否完全等于(==)预期字符串,返回布尔值
2 title_contains : 判断当前页面的title是否包含预期字符串,返回布尔值
3 presence_of_element_located : 判断某个元素是否被加到了dom树里,并不代表该元素一定可见
4 visibility_of_element_located : 判断某个元素是否可见. 可见代表元素非隐藏,并且元素的宽和高都不等于0
5 visibility_of : 跟上面的方法做一样的事情,只是上面的方法要传入locator,这个方法直接传定位到的element就好了
6 presence_of_all_elements_located : 判断是否至少有1个元素存在于dom树中。举个例子,如果页面上有n个元素的class都是‘column-md-3‘,
那么只要有1个元素存在,这个方法就返回True 7 text_to_be_present_in_element : 判断某个元素中的text是否 包含 了预期的字符串 8 text_to_be_present_in_element_value : 判断某个元素中的value属性是否 包含 了预期的字符串 9 frame_to_be_available_and_switch_to_it : 判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False 10 invisibility_of_element_located : 判断某个元素中是否不存在于dom树或不可见 11 element_to_be_clickable : 判断某个元素中是否可见并且是enable的,这样的话才叫clickable 12 staleness_of : 等某个元素从dom树中移除,注意,这个方法也是返回True或False 13 element_to_be_selected : 判断某个元素是否被选中了,一般用在下拉列表 14 element_selection_state_to_be : 判断某个元素的选中状态是否符合预期 15 element_located_selection_state_to_be : 跟上面的方法作用一样,只是上面的方法传入定位到的element,而这个方法传入locator 16 alert_is_present : 判断页面上是否存在alert


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325007552&siteId=291194637