Tonight I was trying to add a new test to validate that TeamMentor’s About page is being displayed correctly.
I thought that this test would take me a few minutes to complete, but the scenario was totally different.
For some reason I was not able to find the About page with WebDriver, even when I was able to see that page displayed. I analyzed the DOM (Document Object Model) and I realize that this page is displayed within an Iframe. Fortunately using Internet Explorer Developer Tools (which is displayed by hitting F12), I was able to discover the existence of this Iframe.
It turns out that in order to interact with the content within an Iframe, a bit of “magic” needs to be done before.
WebDriver does not recognize the Iframe content unless you change the pointer to the Iframe, the following line of code shows how to change the direction the iframe:
This single lines switch to the specific frame.In this case I’m looking at the first frame (the one at the position 0), but here you can use the By interface to find an Iframe by Id or any other mechanism.
Once you have switched to the frame, then it is easy to validate the content.
var aboutPageContent = driver.FindElementex(By.Id("aboutText")); Assert.IsTrue(aboutPageContent.Text.Contains("TeamMentor: Timely. Relevant. Prescriptive.")); Assert.IsTrue(aboutPageContent.Text.Contains("What does TeamMentor help you do?"));
If you want to lose the track of the Iframe, then you can go back to your page by using the switch again but this time you need to set that you want to go back to the DefaultContent:
That is the way WebDriver offers to deal with Iframes :).