8

I have a page including iframe tags, and want to catch if content is fully loaded in the iframe.

I use time.sleep with check document.readyState and it works well in ideal cases ; strong and fast response from web server. But It seems not to guarantee all situations, and I want to improve my code.

Please tell me know some advice or tips. Thanks.

My envs

  • os : windows 7 x64
  • chrome : 68.0.3440.106 (official, 64bit)
  • python : 3.6.6
  • selenium : 3.14.0

I refer below documents.

and i wrote code below

def wait_for_document(self, driver):
    time.sleep(3)
    for i in range(20):
        if driver.execute_script("return document.readyState") == "complete" : return
        else : time.sleep(1)
Ahmad Shakib
  • 455
  • 4
  • 13
IvoryCirrus
  • 622
  • 1
  • 4
  • 15

4 Answers4

10

Try below code to wait for iframe and switch to it to be able to handle inner nodes:

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

driver = webdriver.Chrome() 
driver.get(URL) 
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("iframe_name_or_id"))

Instead of "iframe_name_or_id" you can pass iframe as WebElement:

wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath('//iframe')))

To wait for presence of element inside frame:

wait(driver, 10).until(EC.presence_of_element_located((By.ID, "Element ID")))

You can also use By.NAME, By.CLASS_NAME, By.XPATH, etc...

More about ExplicitWait

Andersson
  • 51,635
  • 17
  • 77
  • 129
0

You need to change your selenium execution to the iframe and check the loaded url after this.

Example:

$frame = $selenium->findElement(WebDriverBy::id('iframe'));
$selenium->switchTo()->frame($frame);

if (strstr($selenium->getCurrentURL(), "my_url") !== FALSE) {
  do stuff...
}
-1
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException

browser = webdriver.Chrome('/path/to/driver')
browser.get('http://your/url')
try:
    myElem = WebDriverWait(browser, delay).until(expected_conditions.presence_of_element_located((By.ID, 'id of iframe or whatever')))
except TimeoutException:
    # error

#page is loaded
-1
you could use this method to  wait for page to fully load:-

public  boolean waitForJStoLoad() {

        WebDriverWait wait = new WebDriverWait(driver, 50);

        JavascriptExecutor js = (JavascriptExecutor) driver;

        ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                try {
                    return ((Long)js.executeScript("return jQuery.active") == 0);
                }
                catch (Exception e) {
                    return true;
                }
            }
        };

        // wait for Javascript to load
        ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver driver) {
                return js.executeScript("return document.readyState")
                        .toString().equals("complete");
            }
        };

        return wait.until(jQueryLoad) && wait.until(jsLoad);
    }