在Selenium C中切换到IFrame时获得异常#

时间:2016-01-11 09:49:38

标签: c# html selenium iframe

我尝试切换到iframe,但是我找到了“ No Such element exception ”。

<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>

在上面的html中,您可以在<div>标记中看到2个iframe。我想导航到最后一个iframe,我想迭代一个表。

我尝试了以下代码。

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)));
            }
        }

我试过了 。driver.switchTo()帧(driver.findelement(By.Id)); 。driver.switchTo()帧(driver.findelement(By.className));

但我得到同样的例外?我不知道我做错了什么。谁能帮我吗? 提前致谢

1 个答案:

答案 0 :(得分:0)

在Selenium中有一个特殊的构造,我喜欢使用它(例子):

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

还有一件事:在HTML中,你不应该依赖类名的顺序。

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

具有相同的含义
<iframe class="stack-calc active body">

由于在XPath中处理起来很复杂,所以应该使用CSS Selector:

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