【问题标题】:Found and switched to an iframe, but cannot find IWebElement in it找到并切换到 iframe,但在其中找不到 IWebElement
【发布时间】:2013-10-21 14:28:23
【问题描述】:

html代码:

<iframe title="javascript:''" src="PageName.aspx" _events="[object Object]">
    <htlml>
        <head>
            <body>
                 <form name="FormName">
                      <div>
                          <span>
                               <input name="ButtonName>

我的问题:如何找到名称为“ButtonName”的元素? 我当前的 C# 代码

//To find the iframe
IWebElement Object = driver.FindElement(By.XPath("XPath to the iframe");   //works
//To switch to and set focus to the iframe
driver.SwitchTo().Frame(Object);  //works

//To find element with name "ButtonName"
IWebElement Button = driver.FindElement(By.Name("ButtonName"));  //error: cannot find the element 

感谢任何帮助。

【问题讨论】:

  • 我猜这个问题是关于Selenium的,应该添加到标签和问题中。
  • 只有“Oject”和“Object”拼写错误才有问题吗?
  • 您好 user1177636,感谢您的评论。可能是帧切换失败的情况。我该如何验证呢?怎样才能让切换成功?

标签: c# iframe selenium


【解决方案1】:

请使用:driver.FindElement(By.Name("ButtonName"));

试试这个代码:

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title></title>
  </head>
  <body>
    <iframe src="Default2.aspx"></iframe>
  </body>
</html>

Default2.aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title></title>
  </head>
  <body>
    <form id="form1" runat="server">
      <div>
        <span>
          <input type="button" name="ButtonName" class="test" onclick="alert(1);" />
        </span>
      </div>
    </form>
  </body>
</html>

我通过 Visual Studio 运行了 Default.aspx。然后,我在控制台应用程序中使用了 Default.aspx 的 URL。我在控制台应用程序的 main 函数中编写了以下代码:

FirefoxDriver driver = new FirefoxDriver
{
    Url = "http://localhost:13764/WebSite1/Default.aspx"
};

IWebElement objecElement = driver.FindElement(By.XPath("//html//body//iframe"));
driver.SwitchTo().Frame(objecElement);
driver.FindElement(By.Name("ButtonName")).Click();

当我运行我的控制台应用程序时,我在我的 Firefox 窗口中看到一个警报,该窗口由上述代码启动。

【讨论】:

  • 感谢您的关注。代码仍然无法在 iframe 中找到按钮。我应该怎么做才能让事情顺利进行?
【解决方案2】:

我昨天回答了similar 的问题。您将元素放在 IWebElement 变量中但没有使用它。

按照用户 197586 指出的那样做:

driver.FindElement(By.Name("ButtonName"));

或者对元素变量做点什么:

IWebElement Button = driver.FindElement(By.Name("ButtonName"));
Button.DoSomething();

如果您只是检查某个元素是否出现,则无需将其分配给 IWebElement 变量。如果您稍后要使用该元素,请确保您实际使用它。

【讨论】:

  • 感谢您的回复。我更正了我的问题,但我不确定为什么我的编辑没有反映在网站上。这是我的代码,它仍然不起作用 IWebElement Object = driver.FindElement(By.XPath("XPath to the iframe"); driver.SwitchTo().Frame(Object); //works IWebElement Button = driver.FindElement( By.Name("ButtonName"));
  • 是的,所以在第二部分你要放置:IWebElement Button = driver.FindElement(By.Name("ButtonName"));如上所述,如果您将 FindElement 分配给 IWebElement 并且不使用 IWebElement,它将找不到您的元素。您需要对 IWebElement 做一些事情或只使用: driver.FindElement(By.Name("ButtonName"));
  • 感谢您的回复。事实上,如果我能找到它,我会使用 IWebElement 变量来验证它是否已启用(Button.Enabled)。另外,我用 iframe 中的其他按钮尝试了您的答案,但仍然找不到该元素。这是实际运行的截图:screencast.com/t/X3K6a6ELgbk 你可以看看我是否遗漏了什么(html 中只有 1 个 iframe,并且按钮在 iframe 中的一个表单上)。谢谢。
  • 我目前无法观看截屏视频,但我只是想知道一些事情。您是否尝试过 CSS 而不是 Name? "输入[name='ButtonName']"
  • 是的,我试过这个代码:IWebElement Button = driver.FindElements(By.CssSelector(".ibtn")); Button.Click();仍然找不到按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 2015-09-11
  • 2022-01-05
  • 2019-07-01
相关资源
最近更新 更多