小肥羊要进步

什么是显示等待和隐式等待?

智能等待 隐式等待:implicitly_wait()

则默认每隔 0.5 秒检查一次,直到 10 秒后超时,如果在 10 秒内完成,则继续执行代码。 

 

显式等待:WebDriverWait()

WebDriverWait(driver,10).until(expected_conditions.presence_o f_element_located((By.ID,'email')))
显示等待,等待邮箱输入框

 

driver.implicitly_wait(3)
隐式等待 3 秒

 

 

什么是显示等待和隐式等待?

显示等待就是有条件的等待
隐式等待就是无条件的等待

显式等待

指定一个等待条件,和一个最长等待时间,程序会判断在等待时间内条件是否满足,如果满足则返回,如果不满足会继续等待,超过时间就会抛出异常

 

input = wait.until(EC.presence_of_element_located((By.ID, 'q')))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))
print(input, button)

隐式等待

当使用了隐式等待执行测试的时候,如果WebDriver没有在DOM中找到元素,将继续等待,超出设定时间后则抛出找不到元素的异常,

browser.implicitly_wait(10)  # 等待十秒加载不出来就会抛出异常,10秒内加载出来正常返回

 

 

Implicit Wait in Selenium

The Implicit Wait in Selenium is used to tell the web driver to wait for a certain amount of time before it throws a “No Such Element Exception”. The default setting is 0. Once we set the time, the web driver will wait for the element for that time before throwing an exception.

Selenium Web Driver has borrowed the idea of implicit waits from Watir.

In the below example we have declared an implicit wait with the time frame of 10 seconds. It means that if the element is not located on the web page within that time frame, it will throw an exception.

To declare implicit wait in Selenium WebDriver:

Implicit Wait syntax:

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);

Consider Following Code:

driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;

Implicit wait will accept 2 parameters, the first parameter will accept the time as an integer value and the second parameter will accept the time measurement in terms of SECONDS, MINUTES, MILISECOND, MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc.

 

Explicit Wait in Selenium

The Explicit Wait in Selenium is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or maximum time exceeded before throwing “ElementNotVisibleException” exception. It is an intelligent kind of wait, but it can be applied only for specified elements. It gives better options than implicit wait as it waits for dynamically loaded Ajax elements.

Once we declare explicit wait we have to use “ExpectedConditions” or we can configure how frequently we want to check the condition using Fluent Wait. These days while implementing we are using Thread.Sleep() generally it is not recommended to use

 

In the below example, we are creating reference wait for “WebDriverWait” class and instantiating using “WebDriver” reference, and we are giving a maximum time frame of 20 seconds.

 

Explicit Wait syntax:

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);
 

posted on 2021-11-22 11:03  小肥羊要进步  阅读(1835)  评论(0编辑  收藏  举报

导航