【问题标题】:How to access dynamically loaded contents of an iframe via selenium webdriver如何通过 selenium webdriver 访问动态加载的 iframe 内容
【发布时间】:2012-11-02 12:48:01
【问题描述】:

我有一个 iframe,它最初是空的,然后在页面上的交互过程中将元素动态加载到 iframe 中。我需要访问其中一个元素才能进入工作流程的下一步。我使用以下代码:-

driver.switchTo().frame(iframeElement);  
Wait wait = new FluentWait(driver).withTimeout(30,TimeUnit.SECONDS).
 pollingEvery(2,TimeUnit.SECONDS).ignoring(NoSuchElementException.class);

WebElement pluginTwitterButton = wait.until(new Function() {
  public WebElement apply( WebDriver driver ) { 
    return driver.findElement( By.id("twitter") ); 
  } 
});

稍后我需要做 pluginTwitterButton.click();

但我收到以下错误 - org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for xxx.xxx.TestCaseSampleReply$1@1217e615 Build info: version: '2.25.0', revision: '17482',时间:'2012-07-18 21:08:56' 系统信息:os.name:'Linux',os.arch:'amd64',os.version:'3.2.0-32-generic',java.version :'1.6.0_24' 原因:org.openqa.selenium.NoSuchElementException:无法定位元素:{"method":"id","selector":"twitter"} 命令持续时间或超时:30.03 秒

我使用 firefox 驱动程序,selenium webdriver jar v.2.25.0 - 当 firefoxdriver 执行时,需要单击的元素是肉眼可见的,但 iframe 变量由于某种原因没有被刷新。我在这里错过了什么?

【问题讨论】:

  • 你试过其他定位器吗?
  • 是的,我有 - 他们都给出了同样的错误。
  • WebElement janRainTwitterButton = (new WebDriverWait(driver, 10)).until(new ExpectedCondition() { @Override public WebElement apply(WebDriver d) { return d.findElement(By.tagName( "选择")); } });
  • 并将其作为输出 - org.openqa.selenium.TimeoutException:等待 10 秒后超时 TestCase.java - 原因:org.openqa.selenium.NoSuchElementException:无法定位元素:{" method":"tag name","selector":"li"} 命令持续时间或超时:36.71 秒 原因:org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"tag名称","选择器":"li"}
  • Vatsala,您确定这不是时序问题吗?在调试这些问题时,最好隔离问题区域。如果您使用断点调试此代码并运行pluginTwitterButton 在页面中看到元素后查找命令?

标签: iframe selenium selenium-webdriver


【解决方案1】:

来自this post,你可以试试这个:

driver.switchTo().frame(iframeElement);
Thread.sleep(2000); // time to load new iframe DOM
WebElement myElement = fluentWait(By.cssLocator(myLocator));

使用这种方法:

public WebElement fluentWait(final By locator) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class, StaleElementReferenceException.class );

    WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    });

    return  foo;
};

【讨论】:

    猜你喜欢
    • 2013-07-26
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    相关资源
    最近更新 更多