Solved

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

Posted on 2008-10-22
2
1,099 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

22 Experts available now in Live!

Get 1:1 Help Now