I believe the below code will get you where you want to go. I've added
appropriate comments for the clever (read: hacky) bits. Note that I've
removed the exception handling; I haven't seen any exceptions using
this method, but that's not to say there might not be some conditions
that would cause them.
Regards,
--Jim
namespace ConsoleApplication2
{
class Program
{
private static List<IWebElement> framePath = new
List<IWebElement>();
static void Main(string[] args)
{
var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("
http://www.failblog.org/");
driver.SwitchTo().DefaultContent();
var dictionary = BuildIframeTree(driver);
}
public static Dictionary<string, object>
BuildIframeTree(IWebDriver browser)
{
Dictionary<string, object> dict = new Dictionary<string,
object>();
ReadOnlyCollection<IWebElement> frameElements =
browser.FindElements(By.TagName("iframe"));
if (frameElements.Count > 0)
{
var childrenDoc = new List<Dictionary<string,
object>>();
foreach (var ifr in frameElements)
{
// We have to start at the root of the document.
WebDriver
// has no mechanism for walking up the frame tree.
driver.SwitchTo().DefaultContent();
foreach (IWebElement frame in framePath)
{
driver.SwitchTo().Frame(frame);
}
string frameSrc = ifr.GetAttribute("src");
// Switch to the frame and add it to the frame
path so
// that when we recurse, we will switch to the
correct
// sequence of frames.
driver.SwitchTo().Frame(ifr);
framePath.Add(ifr);
// Recursion! Yay!
Dictionary<string, object> ifrDoc =
BuildIframeTree(browser);
ifrDoc["src"] = frameSrc;
if (ifrDoc != null)
{
childrenDoc.Add(ifrDoc);
}
// Remove the current child from the frame path so
we
// don't try to switch to it again.
framePath.Remove(ifr);
}
dict["children"] = childrenDoc;
}
return dict;
}
}
}
> Ok, so here's the code.. on thehttp://
failblog.org/page I get 42
> > > > - Show quoted text -- Hide quoted text -