Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Python: How to wait for a page to load after a click?

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()
like image 544
abhanan93 Avatar asked Jun 13 '16 10:06

abhanan93


People also ask

How do you wait for an element to appear in Selenium?

Explicit Wait in SeleniumBy using the Explicit Wait command, the WebDriver is directed to wait until a certain condition occurs before proceeding with executing the code. Setting Explicit Wait is important in cases where there are certain elements that naturally take more time to load.

Which method is used to wait until the page is loaded completely?

ImplicitWait; This will try to wait until the page is fully loaded on every page navigation or page reload.

What is the wait command in Selenium?

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0, meaning disabled. Once set, the implicit wait is set for the life of the session. Java.

What is implicit wait in Selenium Python?

Implicit Waits. An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0 (zero). Once set, the implicit wait is set for the life of the WebDriver object.


1 Answers

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")))
like image 187
wisnia Avatar answered Oct 21 '22 12:10

wisnia