Solved

Call a Javascript function in a webpage using Microsoft.mshtml class in c#.net 2005

Posted on 2008-10-22
2
1,118 Views
Last Modified: 2013-12-17
I need to create an automated system in c# in order to be able to navigate thru Purolator's website since they don't have any webservices to do request to there server.

I am able by using the mshtml.HTMLInputElementClass class to navigate and fill all fields but when I get to adding my package infos, I need to click on a <A HREF> with a onClick in it.

/////
<a href="javascript:LocalAddRow();"><span class="FieldTextLink" id="ctl00_CPPC_lblInsert">Add a Row</span></a>
/////

Does any one has an idea on how I would do it?


Thanks,


///////////////////////////////////////////
//Here is what I normally do with a button input witch works fine. 
 
            do
            {
                el = (mshtml.HTMLInputElementClass)doc.all.item("ctl00$CPPC$btnNext", null);
            }
            while (el == null);
            el.click();
 
 
 
///////////////////////////
//Main part of my Code
 
private void button1_Click(object sender, System.EventArgs e)
        {
            Object o = null;
            Object URL = "https://eshiponline.purolator.com/ShipOnline/SecurePages/Public/FormsLogin.aspx";
            mshtml.HTMLDocumentClass doc;
 
            InternetExplorer ie = new InternetExplorer();
            ie.Visible = true;
            ie.Navigate2(ref URL, ref o, ref o, ref o, ref o);
 
            while (ie.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
            {
                Application.DoEvents();
                System.Threading.Thread.Sleep(50);
            }
 
            // get the document
            doc = (mshtml.HTMLDocumentClass)ie.Document;
 
 
            // Login
            mshtml.HTMLInputElementClass el = (mshtml.HTMLInputElementClass)doc.all.item("ctl00$CPPC$Username", null);
            el.value = "username";
            el = (mshtml.HTMLInputElementClass)doc.all.item("ctl00$CPPC$Password", null);
            el.value = "password";
            el = (mshtml.HTMLInputElementClass)doc.all.item("ctl00$CPPC$imgLogin", null);
            el.click();
 
 
            // Wait till the page is loaded
            el = null;
 
            do
            {
                el = (mshtml.HTMLInputElementClass)doc.all.item("ctl00$CPPC$ctrlShipToAddress$txtName", null);
            }
            while (el == null);
            el.value = "City Name";
 
            do
            {
                el = (mshtml.HTMLInputElementClass)doc.all.item("ctl00$CPPC$ctrlShipToAddress$txtStreetNumber", null);
            }
            while (el == null);
            el.value = "1250";
 
...

Open in new window

0
Comment
Question by:maxleb
2 Comments
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 500 total points
ID: 22784213
0
 

Author Comment

by:maxleb
ID: 22785583
Thanks, it worked great,

Here is, as reference, the code I have used to make it work.

          String code = "LocalAddRow();";
            try
            {
                IHTMLDocument2 doc1 = (IHTMLDocument2)ie.Document;
 
                if (doc1 != null)
                {
                    IHTMLWindow2 parentWindow = doc1.parentWindow;
                    if (parentWindow != null)
                        parentWindow.execScript(code, "javascript");
                }
            }
            catch { }

Open in new window

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 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