gpt4 book ai didi

javascript - 如何将 IEnumerable 转换为常规 HtmlElement

转载 作者:行者123 更新时间:2023-11-28 05:58:25 26 4
gpt4 key购买 nike

我正在尝试使用 VS 2013 中的 WebBrowser 控件在网页上输入文本。我的应用程序是用 C# 编写的。网页的 Html 不使用标准 ID,而是使用称为 data-gel-id 的内容。我相信这是构建网页的公司的习惯,但我对 JavaScript 不够熟悉,无法确定。这意味着我无法使用 GetElementsById() 方法将我的应用程序指向特定文本框。 Html 元素复制如下:

<div class="rcPoint rcPointtext rcPointEditable" data-gel-id="gel_20" style="position: absolute; left: 25.9375%; top: 28.125%; font-size: 12%;"> 

我正在尝试使用下面的代码片段通过 className 获取 Html 元素。该代码基于 MSDN 论坛 post 的片段.

static IEnumerable<HtmlElement> ElementsByClass(HtmlDocument doc, string className)
{
foreach (HtmlElement e in doc.All)
if (e.GetAttribute("className") == className)
{
yield return e;
}
}

但是,当我尝试将此方法的输出分配给 HtmlElement 时,如下所示:

HtmlElement txtbox = ElementsByClass( doc2, "rcPoint rcPointtext rcPointEditable");

我收到一条错误消息,指出您无法将 IEnumerable 显式转换为 HtmlElement。

所以我有几个问题:

  1. 我应该尝试将 IEnumerable 转换为 Html 元素吗?而如果又怎样?
  2. 我是否正确使用了代码片段?我的论点合理吗?
  3. 这是将我的应用程序指向文本框的最佳方式吗?

非常感谢您的帮助,如果我还可以发布其他内容来帮助解答,请告诉我。

最佳答案

如果您知道“自定义 id”,您可能应该尝试这样的操作

var gelId = "gel_20"; // This is the id you know
var txtbox = ElementsByClass( doc2, "rcPoint rcPointtext rcPointEditable")
.FirstOrDefault(x => x.GetAttribute("data-gel-id") == gelId);
if (txtbox != null) {
// Found textbox do something with it
}

关于javascript - 如何将 IEnumerable<HtmlElement> 转换为常规 HtmlElement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37471169/

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