Re: Interacting with Iframes

24 views
Skip to first unread message

darrell

unread,
May 22, 2013, 8:11:38 AM5/22/13
to webd...@googlegroups.com
There are three methods which will switch to a frame. One requires an id or name. One requires an index. One requires a WebElement. So I'd try the three ways.

Method #1: name or id. You state that the id is dynamic. Is it the "AESA-1ZJK5B" portion which is dynamic? I suspect it is. If that is the case than method #1 will not work. 

Method #2: by index. Can you guarantee that the frame will always be the same position in the index? Maybe you can switch to it using:

    driver.switchTo().frame(1);

Methid #3: by WebElement. Rather than finding the web element attribute and using method #1 you can just use the WebElement. The following should work:

    WebElement frameElement = driver.findElement(By.cssSelector("div#workbench>iframe"));
    driver.switchTo(),frame(frameElement);

If this does not work and you still get the NoSuchElement error, you need to look into why it cannot find the element. Maybe the element is being rendered by javascript and your code is trying to find it before the javascript can render it. In these cases you need to do something more like:

    List<WebElement> frameElements = null;
    do {
        Thread.sleep(50);
        frameElements = driver.findElements(By.cssSelector("div#workbench>iframe"));
    } while(frameElements.size() < 1);
    driver.switchTo().frame(frameElements.get(0));

This will check every 50 milliseconds to see if the frame has been rendered. Once the frame is rendered it will exit and switch to the frame.

Another reason switching to the frame will fail is if your locator finds more than one element. You have to make sure your locator will select only one frame. If there are two frames inside the div#workbench, even if the second frame is invisible, you need to find a unique way to identify the frame you want. Often you have to move back to method #2.

On Tuesday, 21 May 2013 09:08:02 UTC-4, Suraj Rajan wrote:

Hi All,

I have a problem in dealing with Iframes in Webpage

here is the HTML code

<div id="workbench" style="display:block">
<iframe id="AESA-1ZJK5BAutoId" class="maxheight" width="100%" height="2124" frameborder="no" src="http://localhost:8082/socialCRM/.." name="AESA-1ZJK5B">
<!DOCTYPE html>
<head>
<title>Problem loading page</title>
Since Iframe id is dynamic, i tried to handle it as like below

String frameId = driver.findElement(By.xpath("//div[@id='workbench']/iframe")).getAttribute("id");
driver.switchTo().frame(frameId);

driver.findElement(By.xpath("//html/head/title")).getText();

Its showing org.openqa.selenium.NoSuchElementException: Unable to locate element: 

Any help is much more appreciated.

Thanks in advance,
 Suraj.
Reply all
Reply to author
Forward
0 new messages