I am trying to navigate on a web browser that has multiple iframes that are all tagged with the id of "InlineDialog_Iframe". When I try to switch to the window I get a stale reference exception, which makes sense because the element that was used before was removed from the page. The HTML gives only a src as an alternate element reference but using this line of code doesn't effectively switch the frame.

IWebElement popUpQuery = driver.FindElement(By.XPath("//iframe[contains(@id, 'InlineDialog_Iframe')and contains (@src, '/AdvancedFind/QueryProperties.aspx?dType=1&feature=dashboard')]"));

What can I do to recognize this iframe element? and what is the best practice for a naming these?

1 Answer 1


Why not just place all the frame handles the driver currently has into a list then you will have all the window handles into a list and can target each individual one.

Or use the findElements method, plural, since they all have the same id.

public void navigateThroughMultipleIframes() {
    String parentWindow = driver.getWindowHandle();

    Set<String> handles = driver.getWindowHandles();
    for (String currentWindow : handles) {

With this, the Set handles holds all the frames the driver currently is holding and then switches to the very last iframe. You can manipulate however you want by modifying the code to go to the specific iframe you want in the Set named handles.

public void l() {
    List<WebElement> iframes = driver.findElements(By.cssSelector("id=InlineDialog_Iframe"));

With this since all your iframes have the same id then the list named iframes holds all the iframes that has that unique id. It is sorted by which the driver finds first.

  • Thanks for your suggestion, can you provide an example?
    – DEnumber50
    Sep 9, 2015 at 14:14
  • @DEnumber50 Sure but I only know it through Java and not C#. Is that okay?
    – Robben
    Sep 9, 2015 at 14:19
  • that is totally fine, I do both; just prefer C#
    – DEnumber50
    Sep 9, 2015 at 14:25
  • The second method will be a better option IF your iframe isn't a popup window. If the iframe pops up as a window then the first method will work.
    – Robben
    Sep 9, 2015 at 14:58
  • 1
    Thanks for your help, I will attempt to implement something like this
    – DEnumber50
    Sep 9, 2015 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.