I want webdriver implicitly wait, after dropdown element has been clicked. Code is below. I have commented the code line where there is code snippet but has no effect
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(r"C:\Users\Admin\Downloads\chromedriver_win32 (1)\chromedriver.exe")
driver.get("https://www1.nseindia.com/products/content/derivatives/equities/historical_fo.htm") #This is a dummy website URL
wait = WebDriverWait(driver, 20)
try:
elem = WebDriverWait(driver, 300).until(
EC.presence_of_element_located((By.ID, "instrumentType")) #dropdown
)
elem.click()
wait = WebDriverWait(driver, 20) #wait implicit doesnot work
finally:
driver.quit()
You definitely misunderstanding what implicit and explicit waits are.
First of all
wait = WebDriverWait(driver, 20)is used for explicit waits.With such
waityou can wait for some condition like presence, visibility, clickability etc. of some element and much more conditions.It is not a pause like
time.sleep(20).time.sleep(20)will pause the program run for 20 seconds whileWebDriverWait(driver, 300).until( EC.presence_of_element_located((By.ID, "instrumentType"))will wait up to 300 seconds to find presence of element located by id="instrumentType". But once it finds such element - it can be instantly - the program flow will continue to the next line instantly.Also, as you can see, each time we use
WebDriverWaitit is for some single, specific condition.Implicit wait (like
driver.implicitly_wait(10)) is set for thedriverobject itself and will be kept for the entire session (until you change it, but we normally do not do it).It defines the timeout for
driver.find_elementanddriver.find_elementsmethods. Again, just a timeout. So, in case an element (or elements) found - the program flow will instantly continue. And in case element was not found till the timeout expiration - exception will be thrown.I mean both implicit wait and explicit wait by
WebDriverWaitwill throw exception, just of different type. In case of no match found.By this:
you creating an object named
waitof typeWebDriverWait. It can be used for explicit waits like above. I mean instead ofYou can do as following: Create a wait object once
and after that to use it as many times as you wish.
So, instead of
you will use
after that you will like to click some element with
then
while you still using the previously created
waitobject.I hope you understand that in this expression
you defined the timeout for the
waitobject to be 20 seconds.