gpt4 book ai didi

c# - 如何使用 Selenium WebDriver 和 C# 查询 iFrame 内部元素的innerHTML?

转载 作者:行者123 更新时间:2023-11-28 19:52:13 24 4
gpt4 key购买 nike

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

enter image description here

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

enter image description here

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# 中的变量?

最佳答案

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

关于c# - 如何使用 Selenium WebDriver 和 C# 查询 iFrame 内部元素的innerHTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23250017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com