?
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,181 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 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

718 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