Solved

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

Posted on 2008-10-22
2
1,133 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Telerik RadEditor Control Save 8 40
PowerShell: ForEach-Object Export to CSV 4 92
Cannot interpret token '"' at position 10. 3 25
SSIS Column mapping 5 42
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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