shayan cardinal
shayan cardinal

Reputation: 69

use time.sleep with webdriver

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??

Upvotes: 1

Views: 104

Answers (2)

Adil kasbaoui
Adil kasbaoui

Reputation: 663

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')))

Upvotes: 1

David Meu
David Meu

Reputation: 1545

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
'''

Upvotes: 1

Related Questions