Q:

Waiting for a Page to Load in Selenium/Firefox w/ Python

How do I wait for a page to load in Selenium before getting its source? I'm trying to get information from the page, but it's loaded with javascript and so just accessing the source doesn't give me anything. I'm currently using time.sleep to wait for the javascript to load, but is there a faster way to do this?
Firefox.get(url)
WebDriverWait(FireFox, timeout=60)
time.sleep(5)
source = Firefox.page_source

I can then search the page's source for the info I need, but is there a faster way? I could make the value 2 seconds or something faster, but then a single slow page could break the program. Removing the time.sleep breaks the program as the javascript doesn't run and the program can't find the info it is looking for.

A:

This waits for the image to be displayed before it fetches the page source.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

browser = webdriver.Firefox()
browser.get('https://vsco.co/vincentdemers/media/573bb798ee5e1d0c03edbe1f')
WebDriverWait(browser, 10).until(lambda d: d.find_element_by_css_selector('#ember610 > img').is_displayed())
source = browser.page_source
browser.close

You can use any method or selector to find the element you wan to wait on.

