【问题标题】:Selenium WebDriver: How to wait for iFrames to load completely?Selenium WebDriver:如何等待 iFrame 完全加载?
【发布时间】:2013-08-31 10:40:14
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: java selenium iframe selenium-webdriver


    【解决方案1】:

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

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

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

    【讨论】:

    • 我用以下代码测试了页面: public void SelectSomething(WebDriver driver, String s) { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); WebDriverWait 等待 = 新的 WebDriverWait(驱动程序, 20); WebDriver frame = wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("frmMain"));等待=新的WebDriverWait(帧,30); System.out.println("#######################" + (new Date())); System.out.println(frame.getPageSource()); System.out.println("#######################" + (new Date())); //////待续……
    • 尝试 { wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName("img"))); } catch (TimeoutException e) { System.out.println(frame.getPageSource()); } System.out.println("#######################" + (new Date())); System.out.println(frame.getPageSource()); System.out.println("#######################" + (new Date())); } //////待续……
    • 有时会得到以下结果:######################Thu Aug 29 10:30:36 CST 2013 #######################Thu 8 月 29 日 10:30:37 CST 2013 PageSource Section#2 ######################Thu Aug 29 10:31:09 CST 2013 PageSource Section#3 ####### ################Thu 2013 年 8 月 29 日 10:31:09 CST //////待续...
    • 这里有两个我无法解决的问题: 1. frame.getPageSource() 获得了整个页面的来源,而不是 iframe 的来源。 2. 我已经等待了足够长的时间让 iframe 中的元素出现,但是使用方法 frame.getPageSource() 获得的源不包括任何人。抱歉,格式很丑。
    • 您是否尝试过先切换到 Iframe,然后等待其中的元素加载?这就是我在我的代码中所做的。在 C# 中,这是通过 driver.SwitchTo().Frame(driver.FindElement(By.CssSelector(IFrameCss))); 完成的
    【解决方案2】:

    验证页面重新加载的最可靠方法是验证元素是否已过时,然后验证新元素是否已加载。

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

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

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

    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);
    

    这对于页面重新加载很有用,因为在页面重新加载时,等待 WebElement 过时可确保元素不再存在,然后等待新元素存在可确保新页面已加载

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 2014-01-03
      • 1970-01-01
      • 2016-11-09
      相关资源
      最近更新 更多