Dynamic iFrame locator - IE and Firefox

The code below is a recursive function to find the iFrame that certain text or elements might be located in. Because the function is recursive, it can search iFrames within iFrames.

The code works as follows: the Custom action needs to use the control target window. A global variable called foundFrame is created and set to blank. Then the function findFrame is executed, and it searches for whatever you insert in the SearchTextHere field. The resulting frame locator is stored in foundFrame.

To use this code, the target window of your action — for example, a click or a FormFill — needs to be set to foundFrame, making sure to use the backticks around the variable name.

A demo script can be downloaded: iFrame Search Example.gsl.

foundFrame = "";
findFrame ("SearchTextHere", "gomez_top[0]");
alert ("Found element in frame: " + foundFrame); 

function findFrame(searchID, currFrame){
	alert ("Current location: " + currFrame);
	var loc = new Locator();
	var currentWin = loc.findFrame(currFrame);
	var frames = currentWin.frames.length;
	alert(currFrame + " has " + frames + " frames.");

	var bodyText = currentWin.document.body.innerHTML;
	if (bodyText.indexOf(searchID) != -1) {
		foundFrame = currFrame;
		alert ("Result : " + foundFrame);
		return true;
	}

	if (frames > 0) {
		for (var i=0; i < frames; i++) {
			findFrame(searchID, currFrame + ".frames[" + i + "]");
		}
	} 

	return true;
}