如何使用SeleniumWebDriver和C#查询iFrame内部元素的innerHTML?

如何使用SeleniumWebDriver和C#查询iFrame内部元素的innerHTML?,c#,javascript,jquery,selenium,C#,Javascript,Jquery,Selenium,我正在测试一个网站,希望输入一个文档的名称,然后搜索具有该名称的id,然后让Selenium WebDriver单击该特定元素以创建文档。问题在于iframe中有一个单独的“Assessments.aspx”页面,用于处理文档的创建。iframe没有id 用于创建文档的元素位于此iframe内部的中。我的目标是查询这个iframe内部的innerHTML,找到传递到方法中的匹配文档名,然后将该id返回给WebDriver,以便它可以单击相应的元素来创建文档 internal bool Crea

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

用于创建文档的元素位于此iframe内部的
中。我的目标是查询这个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;
    }

internal bool CreateDocument(IWebDriver驱动程序、字符串documentName、ref DataObject masterData)
{
尝试
{
var js=作为IJavaScriptExecutor的驱动程序;
如果(js==null)返回false;
//验证我们是否在文档页面上
if(driver.PageSource.Contains(“”)Console.WriteLine(“我们在文档页面上…”);
//进入iframe
FindElement(By.Id(“//iframe[@src='Assessments.aspx']);
驱动程序.切换到().帧(0);
//使用jquery查找文档,然后将其返回到webdriver并单击所需文档的id
const string script1=“var found=$(\“td:contains('Start of Car')\”).attr('id');$('#results').html(found);”;
var元素=(字符串)js.ExecuteScript(script1);
var x=driver.FindElement(By.Id(element));
x、 单击();
返回true;
}
捕获(异常)
{
masterData.Logger.Log(Loglevel.Error,“Boom:{0}”,exception.Message);
}
返回false;
}
如何找到没有id的iframe?如何将javascript函数的结果返回给C#?

中的变量您可以传递
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);";
您可以传递
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);";
您可以传递
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);";
您可以传递
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);";