智能等待 隱式等待:implicitly_wait()
則默認每隔 0.5 秒檢查一次,直到 10 秒后超時,如果在 10 秒內完成,則繼續執行代碼。
顯式等待:WebDriverWait()
顯示等待,等待郵箱輸入框
隱式等待 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);