【问题标题】:How do I query innerHTML for elements that are inside of an iFrame using Selenium WebDriver and C#?如何使用 Selenium WebDriver 和 C# 查询 iFrame 内部的元素的 innerHTML?
【发布时间】:2014-06-08 14:58:15
【问题描述】:

我正在测试一个网站,并希望传入一个文档的名称,然后搜索具有该名称的 id,然后让 Selenium WebDriver 单击该特定元素以创建一个文档。问题是在处理文档创建的 iframe 中有一个单独的“Assessments.aspx”页面。 iframe 没有 id。

用于创建文档的元素位于此 iframe 内的 <div> 中。我的目标是查询此 iframe 中的 innerHTML 以找到传递给该方法的匹配文档名称,然后将该 id 返回给 WebDriver,以便它可以单击相应的元素以创建文档。

internal bool CreateDocument(IWebDriver driver, string documentName, ref DataObject masterData)
    {
        try
        {
            var js = driver as IJavaScriptExecutor;
            if (js == null) return false;

            // verify that we are on the documents page
            if (driver.PageSource.Contains("<div id=\"tabManagementFullPage\">")) Console.WriteLine("We are on the documents page...");

            // get inside the iframe
            driver.FindElement(By.Id("//iframe[@src='Assessments.aspx']"));
            driver.SwitchTo().Frame(0);

            // use jquery to find the document then return it to the webdriver and click the id of the desired document
            const string script1 = "var found = $(\"td:contains('Start of Car')\").attr('id'); $('#results').html(found);";
            var element = (string)js.ExecuteScript(script1);
            var x = driver.FindElement(By.Id(element));
            x.Click();
            return true;
        }
        catch (Exception exception)
        {
            masterData.Logger.Log(Loglevel.Error, "Boom:{0}", exception.Message);
        }
        return false;
    }

如何找到没有 id 的 iframe?如何将 javascript 函数的结果返回到 C# 中的变量?

【问题讨论】:

    标签: c# javascript jquery selenium


    【解决方案1】:

    你可以传递Frame()一个元素。

    IWebElement iframe = driver.FindElement(By.Xpath("//iframe[@src='Assessments.aspx']"));
    driver.SwitchTo().Frame(iframe);
    

    至于javascript部分,你需要返回一个值:

    const string script1 = "return $(\"td:contains('Start of Car')\").attr('id'); $('#results').html(found);";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-06
      • 1970-01-01
      • 2014-06-05
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多