Search code examples
internet-explorercom

how to set a value for form elements


I try to automate a simple process for my customers by calling a site which contains a form and then insert a few values which I already know. Thus the user just need to complete the missing values and submit the form. What I've done so far is starting IE and navigate to the site which contains the form. I'm even able to retrieve the input elements but I can not find a way to set a value for them. If I try to set a value using "Value" as Property/Method name I only receive "Description: 80004001 / Not implemented". I'm stuck at this point.

Using c# with .NET I'm able to accomplish this by doing the following:

SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
IE.Navigate2("http://some.where");
var form = IE.Document.Forms(0);
form.Elements("foo").Value = "bar";
[...]
form.Submit();

But I'm not really sure if I use COM by doing this or some kind of special .NET stuff with more possibilities. However - using COM (from java - but I don't think that this is from relevance) I've this so far:

ActiveXComponent xl = new ActiveXComponent("InternetExplorer.Application");
Dispatch ie = xl.getObject();
Dispatch.invoke(ie, "Navigate2", Dispatch.Method, new Object[] {"http://some.where"}, new int[1]);
// Now we're at http://some.where
xl.setProperty("Visible", new Variant(true));

// Getting the document
Dispatch document = Dispatch.get(ie, "Document").getDispatch();

// At this point I'm not able to call a property or method called "Elements"
// like I did with the c# example above. This makes me believe that my c#
// example is using a more 'integrated' IE-automation as the COM interface does.
// However, reading MSDN documentation I was a able to find a way to get a few sets further:

// Retrieving all input-elements
Dispatch elems = Dispatch.invoke(document, "getElementsByTagName", Dispatch.Method, new Object[] { "input" }, new int[1]).getDispatch();

// elems is now a pointer to a collection I can traverse
// To keep it simple I try to use the first element and do something with it:
Dispatch elem = Dispatch.invoke(elems, "item", Dispatch.Method, new Object[] { 0 }, new int[1]).getDispatch();

// 'elem' is now the first input-Element. To verify I can print out its name (foo):
System.out.println(Dispatch.get(elem, "name"));

// However - the following just fails with "Description: 80004001 / Not implemented".
Dispatch.invoke(elem, "value", Dispatch.Get, new Object[] { "test" }, new int[1]).getDispatch();

Isn't there a way to manipulate HTML-Elements via the COM interface? If it isn't then I need to wrap that stuff with .NET and calling this from my code which makes a .NET runtime on customer side mandatory which I tried to avoid..

Thanks, Martin


Solution

  • Try going thru the Document object instead, find the element using getElementsByTagName (or getElementsById), loop in html element collection and set the value using the setAttribute, specifying the value attribute

    var docu = IE.Document; 
    var htmlElements = docu.GetElementsByTagName("inputTagName");
    
    foreach (HtmlElement htmlElement in htmlElements)
        {
            var name = htmlElement.GetAttribute("name");
            if (name != null && name.Length != 0)
            {
                htmlElement.SetAttribute("value","Test");
            }
        }