Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-22
2
Medium Priority
?
1,219 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

971 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