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() 
4
  • 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 Commented Jun 13, 2016 at 10:40
  • Are you talking about explicit wait? Commented Jun 13, 2016 at 10:47
  • Did you google your own question? I did and the first result is your exact question... obeythetestinggoat.com/… Commented Jul 5, 2016 at 3:51
  • See also Python Selenium - Wait until next page has loaded after form submit - Stack Overflow which has some other (may be better) methods. Commented Dec 9, 2021 at 8:39

5 Answers 5

14

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"))) 
Sign up to request clarification or add additional context in comments.

6 Comments

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 You can wait by anything you can use to find an element.
if you expect more elements with that class you can use presence_of_all_elements_located instead
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/[email protected]/components/driver-component.js:10770)
Can you post your whole code here or pastebin if too long?
|
5

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) ) 

1 Comment

Whats with the yield statement?
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 

1 Comment

Infinite loops are rarely the answer.
0

Load the class for the browser your using and use implicitly_wait. implicitly_wait will wait for whatever element your trying to get to be found. This works great when you end up on a new page form the site you're parsing.

from datetime import datetime from selenium.webdriver import Firefox from selenium.webdriver.common.by import By from selenium.common.exceptions import NoSuchElementException firefox = Firefox() firefox.implicitly_wait(time_to_wait = 120) # in seconds: 2 minutes firefox.get('https://www.google.com/') timestamp = datetime.now().timestamp() try: firefox.find_element(by = By.LINK_TEXT, value = 'aedgsf') except NoSuchElementException: pass print(datetime.fromtimestamp(datetime.now().timestamp() - timestamp).strftime('%M:%S:%f')) 
>>> 02:00:910277 

Comments

-1

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.