Solved

MSHTML Drop Down

Posted on 2004-08-23
3
1,995 Views
Last Modified: 2008-01-09
I'm creating a C# application that will launch a url in Internet Explorer using both the shdocvw and microsoft.mshtml references. After the page loads, I need to automatically enter data into form elements, then press the continue button on the screen. Currently, I'm able to load the page, and populate text fields with data. The program throws a cast exception when I try to set the value for a drop down list. I've looked through the object browser in the mshtml reference, and I'm not able to find anything that looks like it would help me.

I have attached the code below.

using System.Runtime.InteropServices;
using SHDocVw;
using System;
using mshtml;

namespace viClient{
      public class Connector
      {
            public Connector()
            {
            }
            public static void Main(string[] args)
            {
                  object o = null;
                  InternetExplorerClass ie = null;

                  ie = new InternetExplorerClass();      
                  IWebBrowserApp wb = (IWebBrowserApp) ie;
                  wb.Navigate("http://mypage.domain.com?var1=true&var2=1",ref o,ref o,ref o, ref o);

                  while (wb.Busy);
                  HTMLDocument wd = (HTMLDocument)wb.Document;

                  Object input = wd.all.item("SearchCriteria",0);
                  if (input == null )
                        Console.WriteLine("No form SearchCriteria Exists!");
                  // Account Form Element
                  HTMLInputElement polNum = (HTMLInputElement)wd.all.item("Account ",0);
                  Account.value="8736425";
                  // Date of Sale Form Element
                  HTMLInputElement SaleMonth = (HTMLInputElement)wd.all.item("SaleMonth",0);
                  SaleMonth.value="04";
                  // Occurence Form Element
                  HTMLInputElement Occurrence = (HTMLInputElement)wd.all.item("Occurrence",0);
                  Occurrence.value="01";
                  HTMLInputElement Continue = (HTMLInputElement)wd.all.item("theContinueButton",0);
                  Continue.click();                        
            }
      }
0
Comment
Question by:MischiefMadness
  • 2
3 Comments
 

Accepted Solution

by:
aprenot earned 500 total points
Comment Utility
The invalid cast is the fact that you are casting a select element (drop-down list) to an input element.  Try this:

HTMLSelectElement SaleMonth = (HTMLSelectElement)wd.all.item("SaleMonth",0);
SaleMonth.value="04";

If this does not work, you will need to iterate through the Select Element's item collection and set the correct Option Element to be selected:

HTMLSelectElement SaleMonth = (HTMLSelectElement)wd.all.item("SaleMonth",0);
for ( int i = 0; i < SaleMonth.Length; i++ )
{
        HTMLOptionElement SaleMonthOption = (HTMLOptionElement)SaleMonth.item("", i);
        if ( SaleMonthOption.text == "December" )
        {
               SaleMonthOption.selected = true;
               break;
        }
}

That should do it for you.
0
 

Author Comment

by:MischiefMadness
Comment Utility
Thanks for the assistance. I somehow missed the HTMLSelectElement class in the object browser. The code you provided, however, was not what I had to implement. The code I used was

HTMLSelectElement SaleMonth = (HTMLSelectElement)wd.all.item("SaleMonth",0);
SaleMonth.selectedIndex=12; // where 12 = December.

I'm going to award 350 points for pointing me in the right direction. I feel this is fair, but I can debate it if there is an issue.

Thanks!
0
 

Author Comment

by:MischiefMadness
Comment Utility
I'm in a giving mood, take the 500! Thanks again.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now