Search code examples
c#.netwpfmshtml

Setting HTML Text Element value


In a C# WPF program I'am trying to set the value of a HTML Text element which is defined:

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

I tried the following:

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

But I get the following exception:

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)).

I know what it says but I don't know which object I can use to access the Textbox.

What am I doing wrong?


Solution

  • You use the HTML Agility Pack to parse the complete HTML (as received by the WebBrowser control).

    You can query it using XPath syntax and it exposes the HTML in a similar manner to the XmlDocument API.