Skip to content

Instantly share code, notes, and snippets.

@vehrka
Created July 5, 2018 13:52
Show Gist options
  • Save vehrka/5894d20ff05654f04de39dc7c95cb3c5 to your computer and use it in GitHub Desktop.
Save vehrka/5894d20ff05654f04de39dc7c95cb3c5 to your computer and use it in GitHub Desktop.
Selenium wait for a page to load completely
## Stolen from http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load-after-a-click.html
class wait_for_page_load(object):
def __init__(self, browser):
self.browser = browser
def __enter__(self):
self.old_page = self.browser.find_element_by_tag_name('html')
def __exit__(self, *_):
self.wait_for(self.page_has_loaded)
def wait_for(self, condition_function):
import time
start_time = time.time()
while time.time() < start_time + 3:
if condition_function():
return True
else:
time.sleep(0.1)
raise Exception(
'Timeout waiting for {}'.format(condition_function.__name__)
)
def page_has_loaded(self):
new_page = self.browser.find_element_by_tag_name('html')
return new_page.id != self.old_page.id
# with wait_for_page_load(context.browser):
# logoutbtn = context.browser.find_element(By.XPATH, '/html/body/app-root/app-header/div[2]/span/text()')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment