source code
import time
from selenium import webdriver
def wait_for(condition_function):
start_time = time.time()
while time.time() < start_time + 60:
if condition_function():
print("wait: "+str(time.time()-start_time))
return True
else:
time.sleep(0.1)
raise Exception('Timeout waiting')
class wait_for_page_load(object):
def __init__(self, browser):
self.browser = browser
def page_has_loaded(self):
self.new_page = self.browser.find_element_by_tag_name('html')
return self.new_page.id != self.old_page.id
def __enter__(self):
print("enter: "+str(time.time()-start_time))
self.old_page = self.browser.find_element_by_tag_name('html')
print(self.old_page.id)
def __exit__(self, *_):
print("exit: "+str(time.time()-start_time))
wait_for(self.page_has_loaded)
print(self.new_page.id)
browser = webdriver.Firefox()
start_time = time.time()
browser.get('http://xxx.xxx.xxx')
with wait_for_page_load(browser):
print("submit1: "+str(time.time()-start_time))
browser.find_element_by_id("save").click()
print("submit2: "+str(time.time()-start_time))
print("end: "+str(time.time()-start_time))
browser.quit()
Output
$ python3 test.py
enter: 1.2493479251861572
fb3185c8-301d-44e4-9f68-77a7de48d68f
submit1: 1.3207898139953613
submit2: 6.801725625991821
exit: 6.801917314529419
wait: 36.69161534309387
43609471-483b-4c51-ae42-6b4427b5ac83
end: 43.50188207626343
Reference
留言
張貼留言