简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM