必须两次切换相同的iframe才能进入iframe IE

时间:2014-04-23 22:33:57

标签: c# iframe selenium webdriver

我的应用程序必须在IE中使用。我正在自动化测试,其中脚本将按列出的顺序执行以下操作

  • 选择选项'会计'在drop-box" Category"获取与会计相关的选项显示在下拉框"类别"

  • 选择选项'付款"在drop-box" Name"对于页面'会计付款过滤器'显示

  • 验证'员工'此页面中的文本框可见

HTML源前选项'付款'已选中(页面'会计付款过滤器'未显示):

<form id="Main">
    <span id="Entity">
    <div>
       <select id="drop_Category">
         <option value =""/>
         <option value = "Accounting">           
       <select id="drop_Name"> <-!
         <option value =""/>
         <option value ="Payment">

HTML来源AFTER选项&#39;付款&#39;已选中(页面&#39;会计付款过滤器&#39;显示并且有iframe)

<form id="Main">
    <span id="Entity">
    <div class="ig_Control">
         <div class ="ig_content">
             <iframe title ="javascript:''">
                 <html>
                   <body>
                      <form id="Form1">
                         <div id="Panel1">
                             <table id="table1"
                                <tr>
                                    <td>
                                       <input id="Employee">

    <div>
       <select id="drop_Category">
         <option value =""/>
         <option value = "Accounting">           
       <select id="drop_Name"> <-!
         <option value =""/>
         <option value ="Payment">

我的代码最多为&#39;付款&#39;选项已被选中。现在我调用SwitchIframe函数,然后查找并验证文本框:

public static bool IsTextboxVisible (IWebDriver driver, Dictionary of all needed data )
{
     //....
     //Call to switch into iframe
     SwitchIFrame(driver,stringXPath);

     //Verify text-box is visible
     var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(1)); 
     //Script crashes here- can't find element
     var Textbox = wait.Until(d => d.FindElement(By.Id(TexboxID))); 
     return Textbox.Displayed;
}

public static void SwitchIFrame (IWebDriver driver,string strXPath)
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
    var iFrame = wait.Until(d => d.FindElement(By.XPath(strXPath)));
    driver.SwitchTo().Frame(iFrame);
}

即使我增加等待时间,脚本也找不到文本框。然后我尝试找到ID&#34; drop_Name&#34;相反,脚本可以找到该下拉框。这意味着它没有切换到iframe。所以我再次切换SAME iframe:

public static bool IsTextboxVisible (IWebDriver driver, Dictionary of all needed data )
{
     //....
     //Call to switch into iframe
     SwitchIFrame(driver,stringXPath);
     //Call again to switch into the same iframe 
     SwitchIFrame(driver,stringXPath);

     //Verify text-box is visible
     var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(1)); 
     //Script crashes here- can't find element
     var Textbox = wait.Until(d => d.FindElement(By.Id(TexboxID))); 
     return Textbox.Displayed;
}

现在脚本可以找到文本框,但有时仍会给出无法评估XPath或导致web元素的异常。我更新功能SwitchIFrame:

public static void SwitchIFrame (IWebDriver driver,string strXPath)
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
    try
    {
      var iFrame = wait.Until(d => d.FindElement(By.XPath(strXPath)));
      driver.SwitchTo().Frame(iFrame);
    }
    catch (NoSuchFrameException)
    {
       var iFrame = wait.Until(d => d.FindElement(By.XPath(strXPath)));
       driver.SwitchTo().Frame(iFrame);
    }
}

但同样的例外情况有时会在“尝试......”中出现。块。我的问题:

  • 为什么我必须两次切换相同的iframe才能进入一个iframe?
  • 为什么没有尝试......抓住......&#39;在SwitchIframe函数中捕获 异常?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您可能需要稍微切换事件的顺序。基于您拥有的代码,我实际上会等待选择后加载的其他div变得可见。

public static bool IsTextboxVisible (IWebDriver driver, Dictionary of all needed data )
{    
 //Verify text-box is visible
  var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 

  var Textbox = wait.Until(d => d.FindElement(By.ClassName("ig_content")));

  SwitchIFrame(driver,stringXPath);
  return Textbox.Displayed;
}

public static void SwitchIFrame (IWebDriver driver,string strXPath)
{
    //var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
    var iFrame = driver.FindElement(By.XPath(strXPath));
    driver.SwitchTo().Frame(iFrame);
}

更改顺序的原因是这些div连接到原始html文档。假设在加载iFrame时出现这些div,您应该能够切换到新的iFrame文档。如果您的xPath不起作用(IE可能会或可能不能很好地使用它),我会尝试使用交换机中帧的索引位置。这可能更可靠,因为帧没有要查找的ID。

  

driver.SwitchTo()框架(1);

我从switch语句中删除了等待,因为在这种情况下如果iFrame依赖于div加载,你需要做的就是在那里有div并且框架加载了很长时间。