GeckoWebBrowser and HtmlElementCollection

本文关键字:HtmlElementCollection and GeckoWebBrowser | 更新日期: 2023-09-27 18:20:58

我是GeckoBrowser的新手。问题出在我的SetText方法中:

void SetText(string attribute, string attName, string value)
{
    // Get a collection of all the tags with name "input";
    HtmlElementCollection tagsCollection =
            geckoWebBrowser1.Document.GetElementsByTagName("input");
    foreach (HtmlElement currentTag in tagsCollection)
    {
        // If the attribute of the current tag has the name attName
        if (currentTag.GetAttribute(attribute).Equals(attName))
        {
            // Then set its attribute "value".
            currentTag.SetAttribute("value", value);
            currentTag.Focus();
        }
    }
}

但我在这条线上遇到了一个错误:

HtmlElementCollection tagsCollection =
        geckoWebBrowser1.Document.GetElementsByTagName("input");

错误为:

无法隐式转换类型"Skybound.Gecko.GeckoElementCollection"到"System.Windows.Forms.HtmlElementCollection"

有什么办法解决这个问题吗?

GeckoWebBrowser and HtmlElementCollection

GeckoDocument上的GetElementsByTagName方法不返回HtmlElementCollection,而是返回GeckoElementCollection(反过来,它包含GeckoElement s,而不是HtmlElement s)。

所以你需要这样的东西(未经测试):

void SetText(string attribute, string attName, string value)
{
    // Get a collection of all the tags with name "input";
    GeckoElementCollection tagsCollection = geckoWebBrowser1.Document.GetElementsByTagName("input");
    foreach (GeckoElement currentTag in tagsCollection)
    {
        // If the attribute of the current tag has the name attName
        if (currentTag.GetAttribute(attribute).Equals(attName))
        {
            // Then set its attribute "value".
            currentTag.SetAttribute("value", value);
            currentTag.Focus();
        }
    }
}
相关文章:
  • 没有找到相关文章