0

C#WPFプログラムで、次のようTextに定義されたHTML要素の値を設定しようとしています。

<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コントロールによって受信されたもの) を解析します。

構文を使用してクエリを実行するとXPath、同様の方法で HTML がXmlDocumentAPI に公開されます。

于 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 に答える