0

I was watching YouTube video where I'd noticed that the following code:

import time
from selenium import webdriver 

driver = webdriver.Chrome(executable_path="...") # here just creates new Chrome driver

try:
   driver.get(URI)
   time.sleep(3)
   
   respond = driver.page_source
   print(respond)

except Exception as _ex:
   print(_ex)
finally:
   driver.close()
   driver.quit()

But at the same time it says that you don't have to use time.sleep statement. Instead you can use WebDriverWait.

So my question is how could we refactor this code using WebDriverWait and why do we need to use wait here in general?

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
  • Actually, I think using webdriver wait lets your code continues its processing during the waiting but when you use time.sleep() its like you make a space between two clocks of cpu. – Sina M Dec 06 '22 at 13:52
  • Check this question/answer(s): https://stackoverflow.com/questions/59130200/selenium-wait-until-element-is-present-visible-and-interactable – Claudio Batista Dec 06 '22 at 14:31
  • You don't need to use a wait or sleep after a .get(). Selenium will automatically wait for the page to load. The only exception to this would be a "lazy-load" style site where placeholders are populated after the page loads or if you've changed pageloadstrategy. A webdriverwait is more efficient than a sleep because it will only wait as long as needed for the explicit condition to be met. – pcalkins Dec 06 '22 at 20:43

0 Answers0