Issue
I have two questions related to waits. First, please explain to me what is the difference between the two methods of waiting
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID,
'twotabsearchtextbox')))
# and this
driver.implicitly_wait(20)
Note that I wrote a code to see the difference before I asked, but the difference was not clear to me The code you wrote
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import *
driver = webdriver.Chrome()
driver.set_window_position(-1200, 0)
driver.maximize_window()
driver.get("http://www.amazon.com/");
# driver.implicitly_wait(20)
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox')))
search = driver.find_element(By.ID, 'twotabsearchtextbox')
search.click()
search.send_keys('Laptop hp core i5')
The second question is that I sometimes see the WebDriverWait Method assigned to a variable, I mean like this:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox'))))
What is the difference?
I thought WebDriverWait just waits for the item to appear
and the driver.implicitly_wait(20)
function
Waiting for the page to load.
As for what happened to me, the two methods are waiting for the page to load
Solution
Implicit wait
By Implicitly waiting, the WebDriver instance polls the HTML DOM for a certain duration when trying to find any element. This is useful when certain elements on the webpage are not available immediately and need some time to load. Implicit waiting for elements to appear is disabled by default and will need to be manually enabled on a per-session basis. As an example:
driver.implicitly_wait(10)
Explicit wait
Explicit waits allows our code to halt program execution, or freeze the thread, until the condition we pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting. Thus Explicit waits allows us to wait for a condition to occur which inturn synchronises the state between the browser and its DOM Tree, and the WebDriver script. As an example:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'twotabsearchtextbox')))
A word of caution
Do not mix implicit and explicit waits. Doing so can result into unpredictable wait times. As an example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.