Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.



Programming and Learning from SD

I added a lengthy answer to the StackOverflow question on switching focus to an iFrame when using Selenium to automate a UI for testing and thought it deserved a blog post.

First you need to select the iFrame and switch the driver context to it. The iFrame I’m working against doesn’t have an id, and there will only be one on this particular page.

This is an example of using it straight out of the box. I have access to the driver through a static property. I set this when I first told Selenium what type of browser I wanted to use. (example Driver = new FirefoxDriver();)

try
{
    var iFrameElement = Driver.FindElementByTagName("iFrame");
    var driver = Driver.SwitchTo().Frame(this.iFrameElement);    
    var element = driver.FindElement(selector);

    // do what you need with the element
}
finally
{
    // don't forget to switch back to the DefaultContent
    Driver.SwitchTo().DefaultContent();
}

Note: You have to get the information from the IWebElement .Text or .Click for example, before calling Driver.SwitchTo().DefaultContent();

Then I created some extension methods to make this easier to call.

/// <summary>
/// Switch to the IFrame and get the new driver.
/// Call SwitchOutofIFrame after done working with the element.
/// </summary>
/// <param name="driver">the current driver</param>
/// <returns>the IFrame driver.</returns>
public static IWebDriver SwitchToIFrame(this RemoteWebDriver driver)
{
    // http://computerrecipes.wordpress.com/2012/08/23/selenium-webdriver-interact-with-an-element-inside-an-iframe/
    // http://stackoverflow.com/questions/3549584/selenium-2-switching-focus-to-a-frame-that-has-no-name-id
    var iFrameElement = driver.FindElementByTagName("iFrame");
    return driver.SwitchTo().Frame(iFrameElement);
} 

/// <summary>
/// Switch back to the default content.
/// </summary>
/// <param name="driver">the current driver</param>
public static void SwitchOutOfIFrame(this IWebDriver driver)
{
    driver.SwitchTo().DefaultContent();
} 

public static void ClickIFrameElement(this RemoteWebDriver driver, By selector)
{
    var iFrameDriver = driver.SwitchToIFrame();
    try
    {
        iFrameDriver.GetAndWaitForElement(selector).Clik();
    }
    finally
    {
        driver.SwitchOutOfIFrame();
    }
} 

public static string GetTextFromIFrameElement(this RemoteWebDriver driver, By selector)
{
    var iFrameDriver = driver.SwitchToIFrame();
    try
    {
        return iFrameDriver.GetAndWaitForElement(selector).Text;
    }
    finally
    {
        driver.SwitchOutOfIFrame();
    }
} 

/// <summary>
/// Use the WebDriverWait to find an element. If it doesn't find it after before the timeout an exception is thrown.
/// </summary>
/// <param name="driver">The current driver Browser.Driver.</param>
/// <param name="bySelector">how to find the element</param>
/// <exception cref="Exception">if the element isn't found after the timeout</exception>
/// <returns>IWebElement</returns>
public static IWebElement GetAndWaitForElement(this IWebDriver driver, By bySelector)
{
    try
    {
        // give more time on the agents
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(ProcessBase.IsRunningOnTestAgent ? 15 : 5));
        return wait.Until(ExpectedConditions.ElementIsVisible(bySelector));
    }
    catch (Exception e)
    {
        // breakpoint here to retry for debugging.
        Trace.WriteLine(e.Message + " Selector: " + bySelector);
        throw;
    }
}
And this is what my Page.cs that represents a web page can look like with the extension methods:
public string GetTitle()
{
  return Browser.Driver.GetTextFromIFrameElement(By.Id("pageTitle"));
}

public void ClickPrintButton()
{
  Browser.Driver.ClickIFrameElement(By.Id("printButton"));
}

The limitation with the extension methods is that they limit what you can get out of the element, before it switches back the default content. When more is needed, use the first plain vanilla call to switchTo or a combination of the extension methods inline.

Posted on Wednesday, April 2, 2014 11:03 AM .Net , UI Automated Testing , Selenium | Back to top


Comments on this post: Working with IFrame elements and Selenium 2+ in C#

# selenium training
Requesting Gravatar...
Great Information admin thanks For Your Information and Any body wants
learn Selenium through Online for Details Please go through the Link

Best Online Selenium Training by IT Professional Trainers

This Will Helps you aalot.
Left by 123 Trainings on Jul 23, 2014 7:50 AM

# selenium training
Requesting Gravatar...

Great Information admin thanks For Your Information and Any body wants
learn Selenium through Online for Details Please go through the Link

Best Online Selenium Training by IT Professional Trainers

This Will Helps you aalot.
Left by SunIT labs on Aug 23, 2014 7:32 AM

# re: Working with IFrame elements and Selenium 2+ in C#
Requesting Gravatar...
Very nice post.
Thanks!
Left by Morscha Muller on Jan 21, 2015 5:25 AM

# re: Working with IFrame elements and Selenium 2+ in C#
Requesting Gravatar...
Well-elaborated and detailed information.

If anybody want to join Selenium Training with 100% Job support by 12+ years of experienced trainers please contact:

Selenium 2.8.0 Online Training

Just try once, You will never disappoint!
Left by Robert Richards on Feb 02, 2015 1:15 PM

# re: Working with IFrame elements and Selenium 2+ in C#
Requesting Gravatar...
very nice site , this article helps to who wants learn Selenium online training <br>

Best Online Training institute in US<br>

Left by kits trainings on Mar 27, 2015 1:33 AM

# re: Working with IFrame elements and Selenium 2+ in C#
Requesting Gravatar...
I chose Testomato from http://www.testomato.com to do my automated website tests
Left by Testomato on May 04, 2015 4:55 AM

# SAP APO Online Training
Requesting Gravatar...


I’m impressed, I have to admit. Rarely do I encounter a blog that’s both equally educative and engaging, and without a doubt, you’ve hit the nail on the head. The issue is something too few people are speaking intelligently about. I’m very happy that I came across this in my search for something regarding this.
SAP APO Online Training
Left by raju on Dec 10, 2015 5:12 AM

# re: Working with IFrame elements and Selenium 2+ in C#
Requesting Gravatar...
Thank you! Thank You! Thank you!!! You have saved the day! I could not figure out how to get the iframe!
Left by Karen Nickel on May 05, 2016 4:30 PM

# re: Working with IFrame elements and Selenium 2+ in C#
Requesting Gravatar...
Great information,thanks for the detailed post.
Left by sudheerkits on Aug 17, 2016 1:14 AM

# re: Working with IFrame elements and Selenium 2+ in C#
Requesting Gravatar...
Thanks for sharing your info. I truly appreciate your efforts and I am waiting for your next post thank you once again.
Left by sivajikits on Sep 22, 2016 1:25 AM

# re: Working with IFrame elements and Selenium 2+ in C#
Requesting Gravatar...
thanks for sharing this post very informative one keep blogging

Best Selenium Training Institute in Chennai
Left by Selenium Training on Dec 22, 2016 1:35 AM

# online training
Requesting Gravatar...
Very good article. I absolutely appreciate this website. Stick with it!
Left by gotrainings on Dec 26, 2016 5:00 AM

# re: Working with IFrame elements and Selenium 2+ in C#
Requesting Gravatar...
I would like to say thank you for the amazing details and concepts you are sharing in this.
http://www.delhimoods.in/
Left by Alisha on Jul 03, 2017 7:54 AM

# KITS Best Online Training
Requesting Gravatar...
Thank you! Thank You! Thank you!!! You have saved the day! I could not figure out how to get the iframe!
Left by chaitukits on Jul 17, 2017 1:21 AM

Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net