• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Autologin project needs an imagebutton click instead of a text button click (which I fail to implement)

Hi Guyzz!!

Some of you will remember me nagging about the autologin project  I was working on recently, so I'll post my code and point out where I run into a problem:

function TForm1.Go1:boolean;
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
begin
result:=false; //assume no go

 ShellWindow := CoShellWindows.Create;
  // get the running instance of Internet Explorer
  for k := 0 to ShellWindow.Count do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then Continue;
    // QueryInterface determines if an interface can be used with an object
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin try

      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      except
      end;
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        begin
          Document := WB.Document;
          // count forms on document and iterate through its forms
          for m := 0 to Document.forms.Length - 1 do
          begin
            ovElements := Document.forms.Item(m).elements;
            // iterate through elements
            for i := 0 to ovElements.Length - 1 do
            begin
              // when input fieldname is found, try to fill out
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'text') = 0) then
                begin
                  ovElements.item(i).Value := edtUserName.Text;
                end;
              except
              end;
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'password') = 0) then
                begin
                  ovElements.item(i).Value := edtPassword.Text;

                end;
              except
              end;
              // when Submit button is found, try to click
              try
// This is where I'd like to press an imagebutton but all my attempts to grab hold of the d*mn thing failed . The code
// displayed here works with a normal button with a 'text element' > I use this code to login to several webpages and
// as long as the button has this text element (i.e. 'Log in' or 'Log mij in') it works. However I now bumped into the first
// page having an image button and studied Geobuls replies which helped me out the first time but currently I'm
// not using a TWebbrowser.            

if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and   //  < here I d like to identify the image button
                   (CompareText(ovElements.item(i).type, 'SUBMIT') = 0) and  < so type should become: 'image'????
                  (ovElements.item(i).Value = edtButton.Text) then  < and what could I do here to get the right one????
                begin

                  ovElements.item(i).Click;
                  result:=true; // we're a go!

                end;
              except
              end;
            end;
          end;
        end;
      end;
    end;
  end;
end;

// Here's the html from the page btw:

<input type=image name="msuLoginSubmit" src="/images/loginButton.gif" alt="Log in" class=image>

I tried for several hours but couldnt get this thing working and kinda stumbled over the fact I dont use a TWebbrowser otherwise Geobuls code would have helped me out.
I hope its clear if not give me a sign and I'll explain.

My regards and max respect fly out to all-o-ya!!

Peter
0
PeterdeB
Asked:
PeterdeB
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:
Peter,
Ran into the same thing, and came up with the following which seems to work well with buttons, images, etc. It also ensures that the used id/password are fillled out before picking up the submit button/image.

Hope this helps,
Russell

-----


function TForm1.PerformLogin(UserID, Password: String): Boolean;
var  pvShell:       IShellWindows;
     pvWeb2:        IWebBrowser2;
     ovIE:          OleVariant;
     ovDoc2:        OleVariant;
     ovElement:     OleVariant;
     ovInput:       OleVariant;
     ovPassword:    OleVariant;
     ovSubmit:      OleVariant;
     dwCount:       Integer;
     dwFrames:      Integer;
     dwIndex:       Integer;
     bSubmit:       Boolean;
begin

  // Set default result
  result:=False;

  // Create the shell windows interface
  pvShell:=CoShellWindows.Create;
  try
     // Walk the internet explorer windows
     for dwCount:=0 to Pred(pvShell.Count) do
     begin
        // Get the interface
        ovIE:=pvShell.Item(dwCount);
        // QI for the IWebBrowser2
        if (IDispatch(ovIE).QueryInterface(IWebBrowser2, pvWeb2) = S_OK) then
        begin
           // Make sure it is ready
           if not(pvWeb2.Offline) and (pvWeb2.ReadyState = READYSTATE_COMPLETE) then
           begin
              // Check for elements in page
              if Assigned(pvWeb2.Document) then
              begin
                 // Set defaults
                 ovInput:=Unassigned;
                 ovPassword:=Unassigned;
                 ovSubmit:=Unassigned;
                 // Get the document object
                 ovDoc2:=pvWeb2.Document;
                 try
                    // Walk the frames
                    for dwFrames:=0 to ovDoc2.Forms.Length-1 do
                    begin
                       // Walk elements
                       for dwIndex:=0 to ovDoc2.Forms.Item(dwFrames).All.Length-1 do
                       begin
                          // Get element
                          ovElement:=ovDoc2.Forms.Item(dwFrames).All.Item(dwIndex);
                          // Check input
                          if (CompareText(String(ovElement.tagName), 'input') = 0) then
                          begin
                             // Check input
                             if (CompareText(String(ovElement.Type), 'text') = 0) then
                                // Get input
                                ovInput:=ovElement
                             // Check password
                             else if (CompareText(String(ovElement.Type), 'password') = 0) then
                                // Set password
                                ovPassword:=ovElement
                             // Check for submit
                             else if (Pos(LowerCase('submit'), LowerCase(String(ovElement.Name))) > 0) then
                             begin
                                // Must be in the same context as user/password (eg, don't falsely pick up a search button)
                                if (VarType(ovInput) > varEmpty) and (VarType(ovPassword) > varEmpty) then
                                begin
                                   // Have submit
                                   ovSubmit:=ovElement;
                                end;
                             end;
                          end;
                          // Release element
                          ovElement:=Unassigned;
                          // Do we have all elements?
                          if (VarType(ovInput) > varEmpty) and
                             (VarType(ovPassword) > varEmpty) and
                             (VarType(ovSubmit) > varEmpty) then
                          begin
                             // Connect to the interface
                             FEvents.ConnectTo(pvWeb2);
                             // Submit
                             ovInput.Value:=UserID;
                             ovPassword.Value:=Password;
                             ovSubmit.Click;
                             // Set result
                             result:=True;
                             // Break
                             break;
                          end;
                       end;
                       // If success then break
                       if result then break;
                    end;
                 finally
                    // Clear refs
                    ovInput:=Unassigned;
                    ovPassword:=Unassigned;
                    ovSubmit:=Unassigned;
                    ovDoc2:=Unassigned;
                 end;
              end;
           end;
           // Release the interface
           pvWeb2:=nil;
        end;
        // Clear the variant
        ovIE:=Unassigned;
        // If success then break
        if result then break;
     end;
  finally
     // Release the interface
     pvShell:=nil;
  end;

end;

0
 
PeterdeBAuthor Commented:
Hi Russell! Thanks for your response!! You encourage me to fire up Delphi and continue where I left some days ago:)

I will get back to you > for now I'll be testing the code!

Thanks for the effort you put into assisting me!

Regards Peter
0
 
Russell LibbySoftware Engineer, Advisory Commented:
No problem Peter,

Just make sure to pull out the following in the above code:

    // Connect to the interface
     FEvents.ConnectTo(pvWeb2);

as it won't compile for you (clipped the code from another question I helped on).

Let me know if you run into any problems,
Russell

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
PeterdeBAuthor Commented:
Hi Russell!!

Well mind me > I ran into the FEvents problem > but investigated some of the other projects I had been working on which lead me to use the ie_events.pas file > so my guess would be you're the one who helped out Carzzkiss who was assisting me:)

I even got a complete working project back then but at that moment I was facing a totally different problem so I left the project  he sent and moved on with the problem I was focussing on. So I just 'discovered' that the project he sent (coming from you or the both of you) seamlessly logged in at the sites my other project failed to log in. I did not succeed in implementing the code you supplied in this topic but you helped me out anyway since I owuld never have gone looking for that old program since I really didnt know it solved my problem.

Ofcourse I could be wrong assuming you were the one who assisted Carzkiss but the code was almost completely identical nevertheless you get the points to this question since my problem is solved now.

I hope this all doesnt sound to complicated > I just want you to know I didnt get the code you supplied to work but the project I was sent by Carzkiss (with code very very much alike yours or even pressumably yours) deals with my problem and you are the one who encouraged me to find a solution which I found...

My regards a big thank you for the effort you put into assisting me!

I will be posting some more questions regarding this login project Im working on until Im completely satisfied with it.

Max respect fly out to all-o-ya!!

PeterdeB
0
 
Russell LibbySoftware Engineer, Advisory Commented:

I'm sure Wayne will back me up on this, but the code is mine. ;-)

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21216625.html

Glad you have things in order, let me know if you run into other problems, as Im sure I can help you out. (I also wrote the ie_events.pas file)


Kind regards,
Russell
0
 
PeterdeBAuthor Commented:
Ok then I'm talking to the right person >  I will post another  question > a rather huge one but I'll  reward it generously. I feel I've almost reached satisfaction just a few issues which require some attention so now is the moment for me to complete my project:)

Regards from PeterdeB

Ps could I have your emailaddress > t.bommel@chello.nl is mine > I have some questions about your component
0
 
Wayne BarronCommented:
rllibby.
  Yep, the code assist was from Russell.
I like many others on EE, use others information to assist others.
It is the only way to assist and help everyone in the group.
If we did not, then there would be a lot more unanswered posts in EE.
Of which hopefully everyone will be able to assist everyone.
So that no one goes without assistance.

Take Care all;
Carrzkiss
0
 
fdehellCommented:
Such cooperation improves the quality and lifts the level of assistance you can offer to others who are in need of help. I can testify on that! EE in fact relies on that cooperation between wizardzz like you guys!!

Regards and respect!!

Peter
0
 
fdehellCommented:
Aha and to avoid misunderstanding > I accidentally logged in as Fdehell, being Frank my twinbrother who occassionally uses my pc when he visits me. So these replies come from Peter, logged in as Fdehell.


We are twins, the magic touch never ends....:)


Regards Peter
0
 
Wayne BarronCommented:
:-)
Take care. maybe you twins can meet the "Olson Twins" :p

Take Care
0
 
PeterdeBAuthor Commented:
LOL :)

Regards Peter
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now