简体   繁体   中英

Python Sleep is way off

I've written a little python script here and I'm running it from a dos prompt on windows 7 pro (64 bit). The issue is that I'm calling time.sleep(1) and its sleeping for, on average, 30 seconds. Now, I can tolerate differences in the order of miliseconds, but 30x? Thats a little too much to handle. Here's a larger snippit of what I'm doing:

for i in range(10):
    print("start loop at " + str(i))
    try:
        if self.driver.find_element_by_link_text(text).is_displayed():
            break
    except:
        print("pass")
        pass
    time.sleep(1)
else:
    print("failing on timeout")
    self.fail("time out")

All of this (as you might guess) is wrapped inside a class which is being run by unittest. I don't even know where to begin troubleshooting this.

edit: The generated python code from Selenium IDE is garbage...

Most likely time.sleep is not the culprit but the function:

self.driver.find_element_by_link_text(text).is_displayed():

If the 10 second timeout is more important then calling the function 10x you could make a timestamp before running the loop and then see if 10 seconds have passed.

import time

timestamp = time.time()
while time.time() < timestamp + 10:
    print("start loop at " + str(i))
    try:
        if self.driver.find_element_by_link_text(text).is_displayed():
            break
    except:
        print("pass")
        pass
else:
    print("failing on timeout")
    self.fail("time out")

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