16

I want to grab the page source of the page after I make a click. And then go back using browser.back() function. But Selenium doesn't let the page fully load after the click and the content which is generated by JavaScript isn't being included in the page source of that page.

element[i].click()
#Need to wait here until the content is fully generated by JS.
#And then grab the page source.
scoreCardHTML  = browser.page_source
browser.back()
abhanan93
  • 251
  • 1
  • 2
  • 11
  • you could select a specific element and then wait for it to appear before you do your actions, thus letting the entire page load as well. However there may be a better way to this – VlassisFo Jun 13 '16 at 10:40
  • Are you talking about explicit wait? – abhanan93 Jun 13 '16 at 10:47
  • Did you google your own question? I did and the first result is your exact question... http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load-after-a-click.html – JeffC Jul 05 '16 at 03:51
  • See also [Python Selenium - Wait until next page has loaded after form submit - Stack Overflow](https://stackoverflow.com/questions/42069503/python-selenium-wait-until-next-page-has-loaded-after-form-submit) which has some other (may be better) methods. – user202729 Dec 09 '21 at 08:39

4 Answers4

15

As Alan mentioned - you can wait for some element to be loaded. Below is an example code

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Firefox()
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "element_id")))
Vivek
  • 322
  • 1
  • 13
wisnia
  • 646
  • 5
  • 14
  • Can I wait for the presence of element by class name too? 'By.CLASS_NAME'... is that the right syntax to address class names? – abhanan93 Jun 13 '16 at 10:51
  • @abhanan93 You can wait by anything you can use to find an element. – RemcoW Jun 13 '16 at 11:37
  • if you expect more elements with that class you can use presence_of_all_elements_located instead – wisnia Jun 13 '16 at 11:41
  • I am facing this error message while using explicit wait even though the page is now fully loaded and I have fetched the HTML completely. TimeoutException: Message: Stacktrace: at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpQIQukJ/extensions/fxdriver@googlecode.com/components/driver-component.js:10770) – abhanan93 Jun 13 '16 at 15:57
  • Can you post your whole code here or pastebin if too long? – wisnia Jun 14 '16 at 06:14
4

you can also use seleniums staleness_of

from selenium.webdriver.support.expected_conditions import staleness_of

def wait_for_page_load(browser, timeout=30):
    old_page = browser.find_element_by_tag_name('html')
    yield
    WebDriverWait(browser, timeout).until(
        staleness_of(old_page)
    )
3

You can do it using this method of a loop of try and wait, an easy to implement method

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("url")
Button=''
while not Button:
    try:
        Button=browser.find_element_by_name('NAME OF ELEMENT')
        Button.click()
    except:continue
0

Assuming "pass" is an element in the current page and won't be present at the target page. I mostly use Id of the link I am going to click on. because it is rarely present at the target page.

while True:
  try:
    browser.find_element_by_id("pass")
  except:
    break