17

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

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

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

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

4

2 回答 2

18

Select any element on IFrame which takes maximum time to load e.g. 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 !

于 2013-08-28T12:21:46.920 回答
2

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

于 2019-11-26T22:16:08.930 回答