0

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()
xlmaster
  • 659
  • 7
  • 23
  • 1
    What exactly do you want to do after clicking the dropdown? You definitely misunderstanding what implicit and explicit waits are. – Prophet Sep 20 '22 at 16:40

1 Answers1

1

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.

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • great explanation. I was searching for time.sleep() because in VBA wait() method is same for both cases. Thanks, better than documentation Selenium and many other blogs on medium I read. Thanks@Prophet – xlmaster Sep 20 '22 at 17:47
  • I was searching for time.sleep() in my case. But from your question arose my next question. I will be glad, if you will have time to respond. 'wait = WebDriverWait(driver, 20)' we use one time, and in every find_element method driver will use 20s (no more or less) to search for an element. – xlmaster Sep 20 '22 at 17:55
  • 1
    I added more explanations – Prophet Sep 20 '22 at 19:16
  • thanks Prophet, due to your thorough explanation, I got interested and looked also for 'expected condition' class (https://selenium-python.readthedocs.io/waits.html). It is easy now, to know in which circumstance to to use explicit wait while element loads, then click, and where for debugging purpose i can use time.sleep(). Thanks a lot, for your detailed explanations, it boosted to get more though your explanations are detailed and precise. – xlmaster Sep 21 '22 at 10:48
  • 1
    I'm really happy it helped. You are always welcome – Prophet Sep 21 '22 at 10:50