1

I have a problem with time module in use with webdriver. I set time sleep for 3 second but sometime my internet connection will be slow and program miss the element and raise error.

'''
driver.get('exammple.com')
time.sleep(3)
driver.find_element_by_class_name('example')
time.sleep(3)
driver.find_element_by_class_name('example')
'''

how can I handle this problem??

2 Answers2

1

You can do some polling:

'''
driver.get('exammple.com')
found=False
while(not found):
   elem = driver.find_element_by_class_name('example')
   if elem:
       found=True
found=False
while(not found):
   elem = driver.find_element_by_class_name('example')
   if elem:
       found=True
'''
David Meu
  • 1,527
  • 9
  • 14
1

Try this :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
delay = 3 # seconds
myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 
 'IdOfMyElement')))
chikabala
  • 653
  • 6
  • 24