tags:

views:

171

answers:

3

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?

A: 

do you know if you using jquery i can tell you its very easy

$('#tbBName').val('value here');
moustafa
Hey moustafa,i don´t have access to the page´s code and i don´t want to write something in jquery.Its a tool to fill the texboxes of the page with values from a excel file.So you have any c# idea?
Gpx
A: 

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.

Oded
Thank you Oded i will have a look at this pack.But isn´t there a simple way with the code that i am trying?
Gpx
@Gpx - not sure. The one thing I did notice is that you are getting a _collection_ (`getElementsByName` returns a collection) and you are trying to force it into a single element. Try selecting the first result.
Oded
On mshtml.HTMLElementCollection collection = (mshtml.HTMLElementCollection)document.getElementsByName("tbName");i get the same exception.
Gpx
@Gpx - I don't really know. mshtml is old and not very well documented, which is why I suggest using the HTML Agility Pack.
Oded
Thank you for your time Oded, i found an answer from Stan R. in post: http://stackoverflow.com/questions/1133549/mshtml-htmldocumentclass-in-c .I found the element with htmlDoc.all.item("p", 0); Thank you!
Gpx
A: 

I've found getElementsByName to be unreliable when used directly on the document (using it from C++)

So along with the issue mentioned by Oded about the result being a collection, you may want to try something like the following structure. (untested/outline only)

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";
}
Greg Domjan