Aishu
Aishu

Reputation: 1320

Getting an exception while Switching to the IFrame in Selenium C#

I tried to switch to the iframe, but I'm getting the "No Such element exception" found.

<div class="panel1" id="list_container" style="height: 181px;">
  <div class="control" id="FilteredDashboardList">


    <div class="splitter horizontal disabled" id="filteredlist_splitter">
      <div class="panel1" id="views_container" style="height: 23px;">

        <div style="display: none;" class="listheaderbar-bb" id="loading_container">
          <div title="Loading..." class="loading"></div>
        </div>
        <iframe width="100%" height="100%" frameborder="no" id="views_container_frame_2" style="display: block;" src="/WebUI/Editors/CME/Views/ListFilters/DefaultListBar.aspx"></iframe>
      </div>
      <div class="handle">
      </div>

      <div class="panel2" id="list_container" style="height: 157px;">
        <div c:headpath="/WebUI/Editors/CME/Xml/ListDefinitions/ListSearchItems.xml" c:textlookup="true" tabindex="-1" class="list stack horizontal" id="FilteredItemsList" style="outline: 0px none;">
          <div class="head stack-elem" style="display: block; -moz-user-select: none; cursor: default;">
            <div class="container" style="width: 1009px;">
              <div class="column" index="0" style="width: 12px;">
                <div class="text"></div>
                <div class="sort image"></div>
              </div>
              <div class="column" index="1" style="width: 102px;">
                <div class="text">Name</div>
                <div class="sort image"></div>
                <div class="filter" tabindex="0" style="outline: 0px none;"></div>
              </div>
              <div class="column" index="2" style="width: 102px;">
                <div class="text">Task Name</div>
                <div class="sort image"></div>
                <div class="filter" tabindex="0" style="outline: 0px none;"></div>
              </div>
              <div class="column" index="3" style="width: 102px;">
                <div class="text">Publication</div>
                <div class="sort image"></div>
                <div class="filter" tabindex="0" style="outline: 0px none;"></div>
              </div>
              <div class="column" index="4" style="width: 102px;">
                <div class="text">Assigned on</div>
                <div class="sort image"></div>
                <div class="filter" tabindex="0" style="outline: 0px none;"></div>
              </div>
              <div class="column" index="5" style="width: 102px;">
                <div class="text">Assigned To</div>
                <div class="sort image"></div>
                <div class="filter" tabindex="0" style="outline: 0px none;"></div>
              </div>
              <div class="column" index="6" style="width: 102px;">
                <div class="text">Due date</div>
                <div class="sort image"></div>
                <div class="filter" tabindex="0" style="outline: 0px none;"></div>
              </div>
              <div class="column" index="7" style="width: 102px;">
                <div class="text">State</div>
                <div class="sort image"></div>
                <div class="filter" tabindex="0" style="outline: 0px none;"></div>
              </div>
              <div class="column" index="8" style="width: 102px;">
                <div class="text">Owner</div>
                <div class="sort image"></div>
                <div class="filter" tabindex="0" style="outline: 0px none;"></div>
              </div>
              <div class="column" index="9" style="width: 101px;">
                <div class="text">Suspend Or Fail Reason</div>
                <div class="sort image"></div>
                <div class="filter" tabindex="0" style="outline: 0px none;"></div>
              </div>
            </div>
          </div>
          <div class="resizer"></div>
          <iframe width="100%" frameborder="no" tabindex="1" id="FilteredItemsList_frame_details" class="body stack-calc active" src="about:blank" style="height: 141px;">
            <html>
              <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                ....
              </tr>
              <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                .....
              </tr>
              <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                ....
              </tr>
              .....
              .....
            </html>
          </iframe>
        </div>
      </div>
    </div>
  </div>
</div>

In the above html you can see 2 iframes present inside the <div> tag. I want to navigate to the last iframe, where i want to iterate through a table.

I tried the below code.

driver.Navigate().GoToUrl("URL");
Thread.Sleep(60 * 500);
IWebElement containerFrame = A.WaitForElementByXPath(driver, ".//iframe[@class='body stack-calc active']", 60);
            driver.SwitchTo().Frame(containerFrame);
            driver.Close();

    public static class A
        {
            public static IWebElement WaitForElementByXPath(IWebDriver driver, string xPath, int waitTimeInSeconds)
            {
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(waitTimeInSeconds));
                return wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(xPath)));
            }
        }

And I tried driver.switchTo().Frame(driver.findelement(By.Id)); driver.switchTo().Frame(driver.findelement(By.className));

But I'm getting the same exception? I don't know what I did wrong. can anyone help me out? Thanks in advance

Upvotes: 0

Views: 622

Answers (1)

Kim Homann
Kim Homann

Reputation: 3229

There is a special construct for this in Selenium, which I like to use (example):

By iFrameLocator = By.XPath("...");
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(iFrameLocator));

One more thing: In HTML, you should not rely on the order of the class names.

<iframe class="body stack-calc active">

has the same meaning as

<iframe class="stack-calc active body">

As this is complicated to handle in XPath, you should use CSS Selector instead:

By locator = By.CssSelector("iframe.body.stack-calc.active")

Upvotes: 0

Related Questions