Tuesday, January 31, 2017

i Hate iFrames on Selenium C#

On my project i had to face with some elements which is inside a iframe.
So the steps are;

  1. Click on a link (Which opens the iFrame).
  2. Locate some elements inside the iFrame.
  3. Close the iFrame by the close button on the iFrame.
Opening the iframe is Okey, Not a big deal.

This is my Iframe.


Issue happens when locating some elements on it.

I tried with several methods, But nothing works.
Finally i found that i have to switch to the iFrame 1st.


 IWebElement detailFrame = elm.Iframe;
 Driver.SwitchTo().Frame(detailFrame);


Then closing the iFrame.
I tried with pressing the escape key. -But NO luck.

Actions action = new Actions(Driver);
action.SendKeys(OpenQA.Selenium.Keys.Escape).Build().Perform();

Then tryed on clicking on some other area out of the iFrame.
and it works.

But before that i has to switch back to the default frame.

Actions action = new Actions(Driver);
Driver.SwitchTo().DefaultContent();
action.Click(elm.Summary).Build().Perform();

And im Done.



3 comments:

  1. If u do the selenium assignment given to us by u... U will find this scenario... Grrrrr

    ReplyDelete
    Replies
    1. In my case im having a headless iframe.where it dont have close,minimize or maximize buttons.

      Delete