0

In the middle of a webpage I am trying to log into I have the following code

   <tr>
   <td colspan="2" align="center"><!--mstheme--><font face="Trebuchet MS, Arial, Helvetica">

       <br>
       <a href="javascript:SubmitAction()">
          <IMG SRC="images/logon.gif" WIDTH="47" HEIGHT="43" NATURALSIZEFLAG="3" BORDER="0">
       </a>


   <!--mstheme--></font></td>

I want to execute the SubmitAction() code, which will take my credentials and log me in. Using the WebBrowser from .NET works fine, because I can use "invokescript". I cannot do this with shdocvw though. How can I click or otherwise make this action occur, especially since the element has no id or tag?

Here's what my function currently looks like:

        private void webBrowser1_DocumentCompleted(object pDisp, ref object URL)
    {
        while (webBrowser.ReadyState < SHDocVw.tagREADYSTATE.READYSTATE_LOADED) { }
        //we are attempting to log in
        if (loggingIn)
        {
            mshtml.HTMLDocumentClass doc = webBrowser.Document as mshtml.HTMLDocumentClass;                
            doc.getElementById("Username").setAttribute("value", "MLAPAGLIA");
            doc.getElementById("Password").setAttribute("value", "PASSWORD");

            mshtml.IHTMLWindow2 win = doc.parentWindow as mshtml.IHTMLWindow2;
            win.execScript("SubmitAction()", "javascript");

            loggingIn = false;
            return;
        }
mlapaglia
  • 852
  • 14
  • 31

2 Answers2

1
<a href="#" onclick="SubmitAction();return(false);">

This is not the ideal way to do this, its a better pracitce to use an event listenr..

In JQuery it would be

    <a href="#" id="someid">

$('#someid').bind("click", function() { .. do whatever });

That way your not entagling your code logic and your sematic markup.

Stephen
  • 3,341
  • 1
  • 22
  • 21
1

try (EDIT after comment):

using SHDocVw;
using mshtml;


SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
ie.Visible = true;
object o = new object();
ie.Navigate("http://www.google.com", ref o, ref o, ref o, ref o);

while (!(ie.ReadyState >= tagREADYSTATE.READYSTATE_LOADED))
       Application.DoEvents();

var doc = ie.Document;

var win = (IHTMLWindow2)doc.parentWindow;
// here you call the Javascript
win.execScript("SubmitAction();", "javascript");

some interesting links:

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • I have added what my function currently looks like with your code added to the first post. When I run the program the username and password are successfully set, but it doesn't trigger the login function. When I debug the code, the program never makes it past the doc.parentWindow assignment operation.. Any ideas? – mlapaglia Aug 20 '11 at 02:57
  • not sure, check my EDIT - tested this right now and it works for me without a problem – Yahia Aug 20 '11 at 05:15
  • Quite odd, when I put all this code in the same function, it executes correctly. If I add an event handler "webBrowser.DocumentComplete += webBrowser1_DocumentCompleted;", then the code will not execute.. Some debugging will now ensue. – mlapaglia Aug 20 '11 at 18:04
  • It appears even though the while loop is in place to stall until the document is "ready", it isn't ready when it leaves the loop. I run the program with debugging stop points before the win = doc.parentWindow assignment and it will run fine. If I run it just normal debugging with no stop points it fails. The win gets defined to null. I currently have the assingment in another while loop, and it keeps assigning until the win variable isn't null anymore... – mlapaglia Aug 21 '11 at 01:26
  • ok - perhaps you just need to change the while to check for `tagREADYSTATE.READYSTATE_COMPLETE`, that should do the trick IMHO... please don't forget to vote/mark the answer if it helped... – Yahia Aug 21 '11 at 02:02
  • Yes I changed it to READYSTATE_COMPLETE with no change in results. Thanks for the help though you've gotten me a lot farther in my project :) – mlapaglia Aug 21 '11 at 03:04