1

I have an iframe that is initially empty and then later on during the a course of interactions on the page loads elements into the iframe dynamically. I need to access one of these elements to progress to the next step in the workflow. I use the following code:-

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

later i need to do pluginTwitterButton.click();

But I get the following error - org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for xxx.xxx.TestCaseSampleReply$1@1217e615 Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:08:56' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '3.2.0-32-generic', java.version: '1.6.0_24' Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"twitter"} Command duration or timeout: 30.03 seconds

I use firefox driver , selenium webdriver jar v.2.25.0 - the element that needs to be clicked is visible to the eye when the firefoxdriver executes, but the iframe variable doesnt get refreshed for some reason. What am I missing here?

djangofan
  • 28,471
  • 61
  • 196
  • 289
Vatsala
  • 889
  • 1
  • 8
  • 22
  • Have you tried other locators? – Arran Nov 02 '12 at 15:36
  • Yes I have - Alll of them give the same error. – Vatsala Nov 05 '12 at 08:29
  • WebElement janRainTwitterButton = (new WebDriverWait(driver, 10)).until(new ExpectedCondition() { @Override public WebElement apply(WebDriver d) { return d.findElement(By.tagName("select")); } }); – Vatsala Nov 05 '12 at 08:41
  • And got this as output - org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting TestCase.java - Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"tag name","selector":"li"} Command duration or timeout: 36.71 seconds Caused by:org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"tag name","selector":"li"} – Vatsala Nov 05 '12 at 08:49
  • Vatsala,have you ensured that this is not a timing issue?While debugging these issues, it would be better if you isolate the problem area.What is happening if you debug this code with breakpoint and run the pluginTwitterButton find command after you see the element in the page? – A.J Nov 05 '12 at 16:31

1 Answers1

2

From this post, you could try this:

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

Using this method:

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;
};
Community
  • 1
  • 1
djangofan
  • 28,471
  • 61
  • 196
  • 289