【问题标题】:Must switch the same iframe twice to get inside the iframe IE必须切换相同的 iframe 两次才能进入 iframe IE
【发布时间】:2014-04-23 22:33:56
【问题描述】:

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

  • 在下拉框“类别”中选择选项“会计”以在下拉框“类别”中显示与会计相关的选项

  • 在“名称”下拉框中选择“付款”选项以显示“会计付款过滤器”页面

  • 验证此页面中的“员工”文本框是否可见

选择“付款”选项之前的 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 源代码(显示“会计付款过滤器”页面并且有一个 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">

我已经选择了“付款”选项的代码。现在我调用 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“drop_Name”,脚本可以找到该下拉框。这意味着它没有切换到 iframe。所以我再切换一次相同的 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);
    }
}

但是在“try...”块中有时仍然会发生相同的异常。我的问题:

  • 为什么我必须切换同一个 iframe 两次才能进入一个 iframe?
  • 为什么 SwitchIframe 函数中的 'try...catch...' 没有捕获 例外?

非常感谢任何帮助。

【问题讨论】:

    标签: c# iframe selenium webdriver


    【解决方案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().Frame(1);

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多