after clicking the element implicitly wait

Question:

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:UsersAdminDownloadschromedriver_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()
Asked By: xlmaster

||

Answers:

You definitely misunderstanding what implicit and explicit waits are.
First of all wait = WebDriverWait(driver, 20) is used for explicit waits.
With such wait you 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 while WebDriverWait(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 WebDriverWait it is for some single, specific condition.
Implicit wait (like driver.implicitly_wait(10)) is set for the driver object 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_element and driver.find_elements methods. 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 WebDriverWait will throw exception, just of different type. In case of no match found.
By this:

wait = WebDriverWait(driver, 20)

you creating an object named wait of type WebDriverWait. It can be used for explicit waits like above. I mean instead of

WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "instrumentType"))

You can do as following:
Create a wait object once

wait = WebDriverWait(driver, 20)

and after that to use it as many times as you wish.
So, instead of

WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "instrumentType"))

you will use

wait.until(
EC.presence_of_element_located((By.ID, "instrumentType"))

after that you will like to click some element with

wait.until(EC.visibility_of_element_located((By.XPATH, comments_xpath)))

then

wait.until(EC.element_to_be_clickable((By.XPATH, element_path))).click()

while you still using the previously created wait object.
I hope you understand that in this expression

wait = WebDriverWait(driver, 20)

you defined the timeout for the wait object to be 20 seconds.

Answered By: Prophet