0

在 C# WPF 程序中,我试图设置定义的 HTMLText元素的值:

<input name="tbBName" type="text" id="tbBName" tabindex="1" />

我尝试了以下方法:

mshtml.HTMLDocument doc = (mshtml.HTMLDocument)webBrowser1.Document;
mshtml.HTMLInputTextElement tbName = (mshtml.HTMLInputTextElement)doc.getElementsByName("tbBName");
tbName.value = "Test";

但我得到以下异常:

Unable to cast COM object of type 'System.__ComObject' to interface type 'mshtml.HTMLInputTextElement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F520-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

我知道它在说什么,但我不知道我可以使用哪个对象来访问文本框。

我究竟做错了什么?

4

3 回答 3

0

你知道如果你使用 jquery 我可以告诉你它很容易

$('#tbBName').val('value here');
于 2010-05-16T06:43:58.770 回答
0

您使用HTML Agility Pack来解析完整的 HTML(由WebBrowser控件接收)。

您可以使用语法查询它,它以与APIXPath类似的方式公开 HTML 。XmlDocument

于 2010-05-16T06:55:13.863 回答
0

我发现直接在文档上使用 getElementsByName 是不可靠的(从 C++ 使用它)

因此,除了 Oded 提到的关于结果是集合的问题外,您可能还想尝试以下结构。(未经测试/仅大纲)

mshtml.HTMLDocument doc = (mshtml.HTMLDocument)webBrowser1.Document;
mshtml.ElementCollection col = doc.getAll();
Dispatch disp = col.namedItem("tbBName");
// in C++ this can return either a collection or an item
try{ // collection
  mshtml.ElementCollection col2 = (mshtml.ElementCollection)disp;
  for( index = 0; index < col2.length; ++index ) {
    mshtml.HTMLInputTextElement tbName = (mshtml.HTMLInputTextElement)col2[index];
    tbName.value = "Test";
}
try{ // item
    mshtml.HTMLInputTextElement tbName = (mshtml.HTMLInputTextElement)disp;
    tbName.value = "Test";
}
于 2010-06-07T20:05:47.273 回答