selenium. shows the wait (WebDriverWait)

Syntax show waiting

WebDriverWait(driver,timeout,poll_frequency,ignored_exceptions)

driver: 传入WebDriver实例,即webdriver.Chrome()
timeout: 超时时间,等待的最长时间(同时要考虑隐性等待时间)
poll_frequency: 调用until或until_not中的方法的间隔时间,默认是0.5秒
ignored_exceptions: 忽略的异常,如果在调用until或until_not的过程中抛出这个元组中的异常, 则不中断代码,继续等待,如果抛出的是这个元组外的异常,则中断代码,抛出异常。默认只有NoSuchElementException

WebDriverWait wait two ways

until(method,message)
method: 在等待期间,每隔一段时间(__init__中的poll_frequency)调用这个传入的方法,直到返回值不是False
message: 如果超时,抛出TimeoutException,将message传入异常
until_not(method,message)
与until相反,until是当某元素出现或什么条件成立则继续执行,
until_not是当某元素消失或什么条件不成立则继续执行,参数也相同,不再赘述。

Call the method: WebDriverWait (driver, long timeout, call frequency, ignore the exception) .until (executable method, the information returned timeout)

Here demo is operating in accordance with the input box until Baidu and until_not

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

browser = webdriver.Chrome()
browser.get("https://www.baidu.com")

# 一行代码完成
WebDriverWait(browser,10).until(lambda browser:browser.find_element_by_id("kw")).send_keys("pytest")

# 定义方法完成
def kw(driver,times,func):
    return WebDriverWait(driver,times).until(func)
kw(browser,10,lambda x:x.find_element_by_id("kw")).send_keys("selenium")

Guess you like

Origin www.cnblogs.com/youngleesin/p/11390606.html