justuno
justuno

Reputation: 43

driver.get() takes too long to finish, How can I set a timeout method to deal with it?

A website I need to test usually takes much time to load, I don't want to wait when I need to test it, so I spend almost a whole day in searching answers from the Internet. However, I still not finding out a perfect answer to solve this problem.

the essential of my problem is opening a new page, such as using driver.get(), takes too much time to load completely, though the element I expected was appeared, I should click the stop button of the chrome several times manually when I ran my script.

here is the method I have tried before.

def timeout(func):
def wrapper(*args, **kwargs):
    try:
        driver.set_page_load_timeout('10')
        func(*args, **kwargs)
    except Exception:
        print('This fucking function takes too long to finish!')
    finally:
        driver.execute_script('window.stop();')
return wrapper

And I used it in this way:(for example)

url = 'http://igame.163.com'
@timeout
def openwebsite(url):
    driver.get(url)
openwebsite(url)

However, the script often threw out the timeout exception,or even no such element, or not clickable in other places, if I delete these code above, none of exceptions will be threw.

I just want to find a way that when loading a page takes too much time, more than the timeout I set before, the script will click the stop button of the chrome automatically, I'm sure that all the elements I need has been loaded completely.

PS:I use python 3.4 to write selenium scripts, browser is chrome,and my OS platform is Win7 X64.

Upvotes: 0

Views: 1145

Answers (1)

cinatic
cinatic

Reputation: 969

Did you tried: set_script_timeout

If i did understand then you want to break always after [n] seconds and not only if the request to load the page need more than [n] seconds.

Upvotes: 0

Related Questions