简体   繁体   中英

Selenium WebDriver: How to wait for iFrames to load completely?

I'm testing a page with an iFrame whose contents are generated by JavaScript dynamically. I have to wait for the iFrame loaded completely to make sure that all the elements are present. I tried the following code, it didn't work.

WebDriver frame = wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("frmMain"));

I also tried to wait for some element in the iFrame to be present. It didn't work, neither.

Any help would be greatly appreciated, thanks!

Select any element on IFrame which takes maximum time to load eg any button or image and the wait using the following code.

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated((By
                .name("element"))));

Or rather you can wait for for iFrame to appear and then switch to it and then use the above statement !

The most robust way to verify a page reload is to verify that an element went stale and then to verify that a new element has loaded.

Choose an element, elementToBeStale , which exists on the "old" page. This is the element you expect to go stale when the page reloads.

Now choose an element you expect to appear on the page once it has reloaded, xPathOfElementToLoad . Note that this can be the same element you expected to go stale.

Note that xPathOfElementToLoad can't be a WebElement because the it doesn't yet exist on the page so it can't be represented by one.

WebDriverWait wait = new WebDriverWait(driver, WAIT_TIME, POLL_INTERVAL);

ExpectedCondition<Boolean> cond1 = ExpectedConditions.stalenessOf(elementToBeStale);
ExpectedCondition<WebElement> cond2 = ExpectedConditions.presenceOfElementLocated(By.xpath(xPathOfElementToLoad));
ExpectedCondition<Boolean> cond = ExpectedConditions.and(cond1, cond2);

wait.until(cond);

This is useful for page reloads because on a page reload waiting for a WebElement to go stale guarantees the element no longer exists and then waiting for a new element to exist guarantees the new page has loaded

I don't know if you already solved this, but here it goes.

Your code is missing the type of search for the element, and I think is "By.ID", so your code should be:

WebDriver frame = wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt((By.ID, "frmMain")))

您可能只需要添加一条等待语句-持续一定的时间...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM