【问题标题】:How to locate and click on an element which is nested within multiple frame and frameset through Selenium using Webdriver and C#如何使用 Webdriver 和 C# 通过 Selenium 定位并单击嵌套在多个框架和框架集中的元素
【发布时间】:2018-11-20 18:36:27
【问题描述】:

我有如下所示的 html 页面,我需要在 clslogin 类中点击登录。

如何遍历查找登录。 我正在使用 C# 和 selenium Webdriver。

使用 XPath (/html/body/div/table/tbody/tr[1]/td[3]/a) 我无法控制登录类,总是抛出未找到元素的错误。任何人都可以帮我获得确切的 xpath。

<html>
 <head></head>
 <frameset name="mytestTopFrame" rows="*"......  >
  <frame name="mytestTopsubframe" src="index.html" width="100%"......... >
   <html>
    <head></head>
     <frameset name="mytest" rows="70,20..."......  >
       <frame name="mytestsubframe" src="menu.html" width="100%"......... >
       <html>
        <body class="clsmenu" .......>
         <div align="left">
           <table id="Title" ......>
            <tbody>
             <tr class="toptitle" ...>
              <td class="clicklogin" ....>
               <a class="clslogin" href="linkref">Login </a>
              </td>
             </tr>
            </tbody>
          </table>
         </div>
        </body>
       </html>
      </frame>
    </frameset>
   </html>
  </frame>
 </frameset>
</html>

【问题讨论】:

    标签: c# selenium selenium-webdriver webdriver frame


    【解决方案1】:

    您需要先更改为正确的 iframe 才能访问 iframe 下方的路径。为此,您可以使用driver.SwichtTo().Frame("your frame ID")。如果此解决方案不起作用,则在此线程 may be a solution 中,该线程使用相同的代码行,但它会搜索父节点和子节点

    【讨论】:

    • 不幸的是,我在框架集中没有“id”属性。相反,我有“名称”属性,我仍然无法控制进入框架。我使用 By.Name("mytestTopFrame") 切换到框架。是否可以使用“名称”属性切换到框架?
    • 是的,它使用“By”,你可以将它与 xpath、name、id 或任何你需要的东西一起使用。
    【解决方案2】:

    根据您共享的 HTML,要单击文本为 Login 的元素,您必须诱导 WebDriverwait 两次才能切换到 2 child frame 然后再次定位所需元素,如下所示:

    //SwitchTo mytestTopsubframe
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Name("mytestTopsubframe")));
    //SwitchTo mytestsubframe
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Name("mytestsubframe")));
    //Locate the desired element
    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='clslogin' and @href='linkref']"))).Click();
    

    注意:您不必考虑&lt;frameset&gt; 的存在,可以放心地忽略这些标签。

    【讨论】:

    • 仍然无法登录。对于每一帧,我都会收到“没有这样的帧”错误。这是浏览器 bcz 的问题吗?我使用的是 Firefox 当前最新版本“Quantum”。
    • 抱歉,在切换到帧之前我没有设置为 DefaultContent()。谢谢它对我有用。 +1
    • 另一个更新。以上代码仅适用于 IE11 版本 11.492。 Firefox、Chrome 最新版本上的“Nosuchframe”异常。知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    相关资源
    最近更新 更多