Article by Amir Ghahrai
If you have been testing web applications with Selenium RC you would have noticed that things tend to get a bit complicated when you’re dealing with IFrames in the web application. In particular when there are nested IFrames and you need to verify that if a certain text exist, you need to know which IFrame the text is supposed to appear before you can verify if the text exists with isTextPresent() function.
When isTextPresent() function is called selenium returns false even when the text is clearly visible on the web page. This is because the text may be inside an IFrame and selenium is only “looking” at the root level of the page; we need to specifically tell selenium to look inside the IFrame to find the text.
There are various ways of selecting an IFrame with selenium and accessing data within the IFrame: By Name: selenium.selectFrame(“mainFrame”); //name of the IFrame
By DOM: selenium.selectFrame(“dom=window.frames[1]“); //second IFrame By Index: selenium.selectFrame(“index=1″); //second IFrame
Remember index starts at 0, so “index=1″ will select the second IFrame. We can call the selectFrame() function multiple times to select the nested IFrames:
selenium.selectFrame(“index=1″);
selenium.selectFrame(“index=1″); will select the second IFrame within the second IFrame from the root e.g
So, what if we didn’t know the name of the IFrame or we didn’t want to hard code the name of the IFrame in the program? Well, the following code shows how to do this.
First it will identify how many IFrames there are on the rendered page. Then it will select each IFrame and then again identify how many IFrames there are inside the selected IFrame. Once we know how many IFrames and nested IFrames we have on the page, it will then loop through each IFrame and its sub IFrames to find the text that we are trying to verify.
We can also use selenium.selectFrame(“relative=up”) to move one IFrame up a level or selenium.selectFrame(“relative=top”) to select the top level IFrame.
package com.company;
import com.thoughtworks.selenium.Selenium;
public class Wait {
static boolean exists = false;
static boolean found;
public static int getNumberOfElements(Selenium selenium, String element) {
return selenium.getXpathCount(element).intValue();
}
public static boolean tillTextPresent(Selenium selenium, String txt){
//selenium.waitForPageToLoad("30000");
found = false;
int i=0;
int x;
int y;
do{
Wait.seconds(1);
exists = selenium.isTextPresent(txt);
if(exists) {
found = true;
}
i++;
//found = false;
if(i == 3) {
x = getNumberOfElements(selenium, "//iframe");
for(int j=0; j< x-1; j++) {
selenium.selectFrame("index="+(j+1));
y = getNumberOfElements(selenium, "//iframe");
for(int k=0; k< y; k++) {
selenium.selectFrame("index="+k);
exists = selenium.isTextPresent(txt);
if(exists) {
k = y+1;
j = x+1;
found = true;
}
else {
selenium.selectFrame("relative=up");
i = 2;
}
}
exists = selenium.isTextPresent(txt);
if(exists) {
j = x+1;
i = 4;
found = true;
}
else {
selenium.selectFrame("relative=up");
}
}
exists = true; //This is to get out of the loop
}
selenium.selectFrame("relative=top");
}
while(!exists);
return found;
}
}

{ 3 comments… read them below or add one }
Hi,
I am facing one problem with iframe as the frame name itself is randomly generated number hence it will be different every time popup window appears.
This is the code which generates iframe name
//<![CDATA[
ord=Math.random()*10000000000000000;
document.write('');
//]]>
Hi,
I used the following to type in iFrame:
selectFrame(“Name/ID of the frame “);
type(“//body”,” how are you? this will print in next line”);
selectWindow(“null”);
Thanks,
Jaya.
We are using PeopleSoft & selenium combination..Here we are not able to access iframe elements. After click on the lookup button,new frame gets opens and we need to select the element from the newly opened frame.
Below code got recorded during script recording and while executing, getting the error as “element xyz not found”.Due to this unable to proceed further.
selenium.selectFrame(“popFrame“);
selenium.click(“link=xyz”);
selenium.selectFrame(“relative=up”);
Can you pls suggest in this case..