Java Selenium WebDriver:如何等待 iFrame 完全加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18486511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:04:31  来源:igfitidea点击:

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

javaseleniumiframeselenium-webdriver

提问by user2432405

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.

我正在测试一个带有 iFrame 的页面,其内容是由 JavaScript 动态生成的。我必须等待 iFrame 完全加载以确保所有元素都存在。我尝试了以下代码,但没有用。

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!

我还尝试等待 iFrame 中的某些元素出现。它没有工作,也没有。

任何帮助将不胜感激,谢谢!

回答by Abhishek Singh

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

选择 IFrame 上需要最长时间加载的任何元素,例如任何按钮或图像,然后使用以下代码等待。

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 !

或者更确切地说,您可以等待 iFrame 出现,然后切换到它,然后使用上面的语句!

回答by Archmede

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.

选择一个elementToBeStale存在于“旧”页面上的元素。这是您希望在页面重新加载时失效的元素。

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.

现在选择您希望在重新加载后出现在页面上的元素xPathOfElementToLoad。请注意,这可能是您希望过时的相同元素。

Note that xPathOfElementToLoadcan't be a WebElementbecause the it doesn't yet exist on the page so it can't be represented by one.

请注意,xPathOfElementToLoad不能是 a,WebElement因为它在页面上尚不存在,因此不能用 1 表示。

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 WebElementto go stale guarantees the element no longer exists and then waiting for a new element to exist guarantees the new page has loaded

这对于页面重新加载很有用,因为在页面重新加载时等待一个WebElement变旧保证元素不再存在,然后等待新元素存在保证新页面已加载