【发布时间】: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