Stand4Unborn
Stand4Unborn

Reputation: 351

Switching to Parent Frame from iFrame and finding an element in Parent frame using Selenium Webdriver. C#

Scenario: - I have a page with an iFrame Text Editor and a button in the page too. - I switched from the parent frame to the iFrame to read from the Text Editor body - After reading from the body of the Text Editor, I want to click on the button in the parent frame of the page. - For this I tried to switch back to the parent frame from the iFrame using the following statement: webDriver.SwitchTo().DefaultContent(); - But still I am not able to find the button element which resides in the parent frame.

I appreciate your help! Thanks

Upvotes: 19

Views: 42579

Answers (3)

Stand4Unborn
Stand4Unborn

Reputation: 351

Thanks for your responses guys. It is solved!

The solution:

  • When I had used the webDriver.SwitchTo().DefaultContent(); it switched the webDriver to the top most window of the page. [Previously I was looking for the button element in this window and therefore was not able to find it as the button was sitting in the main frame of the page]

  • After switching to the main window, I switched the webDriver again to the main frame of the page. This main frame had the button element. Thus I was able to find the button element. And this solved the issue!

So the final code doesn't have webDriver.SwitchTo().DefaultContent(); but has the following in its place:

    _webDriver.SwitchTo().Window(windowHandle);
    _webDriver.SwitchTo().Frame("mainFrame");

Note: windowHandle in the above code is the handle of the top most window of the page. I guess its value may change according to the browsers, not sure though.

Upvotes: 16

Shivam Bharadwaj
Shivam Bharadwaj

Reputation: 2294

The following code worked fine:

driver.switchTo().parentFrame();

Upvotes: 11

DevDave
DevDave

Reputation: 6918

I was struggling with a similar problem and found that I could switch back by Window Handle:

string currentWindow = Driver.CurrentWindowHandle;
// switch to frame and do stuff..
Driver.SwitchTo().Window(currentWindow); // switch back

Upvotes: 8

Related Questions