🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.web
26 messages
26 total messages Started by mike Fri, 05 Jan 2007 21:11
Help with TCppWebBrowser - Find and click a button
#459
Author: mike
Date: Fri, 05 Jan 2007 21:11
86 lines
3856 bytes
Hello, and Happy New Year to all.

I have a problem which I cannot seem to solve, and I do not know how to go 
about it. So if someone could help I would apprciate it very much.

The problem:

I would like to programmably be able to click a button on a web page loaded 
into a TCppWebBrowser. The web page has a Tree View, I believe, on the left 
side and the button is located within this view. If I view the source from 
within the browser the source looks like below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>

<head>
<title>Links Screen</title>
<meta HTTP-EQUIV="Pragma" CONTENT="No-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="No-cache">
<meta HTTP-EQUIV="Expires" CONTENT="0">
<meta HTTP-EQUIV="last modified" CONTENT="NOW">

<script LANGUAGE="JavaScript" SRC="/rpa/default/newAuthlib.js"></script>

<script language=javascript>
if (document.layers) {
  WW = innerWidth; WH = innerHeight;
  captureEvents(Event.RESIZE);
  onresize=function(){if 
(WW!=innerWidth||WH!=innerHeight){location.reload();return 0;}}
}
</script>

<base target="feature">
</head>

<body BGCOLOR="#FFCC33" Link="#800000" vLink="#800000" aLink="#000000"
 text="#000000" topmargin="0" leftmargin="0" marginheight="0" 
marginwidth="0">

<table height="100%" border="0" cellspacing="0" cellpadding="14" 
width="100%">
  <tr>
    <td valign="top"><font FACE="Arial, Verdana"><b><small>Authorized 
Links</small></b></font><p><font face="Arial, Verdana" size="2">
<ul>
<script LANGUAGE="JavaScript">
<!-- Hide script from older browsers.
buildLinks();
//alert('made it');
// End of the hiding here. -->
</script>
</ul></p>
    <form>
      <p><input TYPE="button" NAME="secure_exit" VALUE=" Finished " 
onClick="logout();"> <!--webbot bot="HTMLMarkup" 
startspan --></SCRIPT><!--webbot bot="HTMLMarkup" endspan --> </font></p>
    </form>
    </td>
  </tr>
  <tr>
    <td valign="bottom"><font face="Arial, Verdana" size="2"><p 
align="center"><img src="/rpa/default/images/epix_logo.gif"></p></font></td>
  </tr>
</table>
<!--<p><font face="Verdana" size="1">the following link is temporary... 
</font></p><form action="http://subscribe.eb.com/cgi-bin/institution_rem.pl" 
method="post">  <input type="hidden" name="uid" value="!@#als"><p><input 
type="submit" value="Britannica">  </p></form><p>&nbsp;</p>-->
</body>
</html>

I would like to be able to auto click the button above or invoke the 
logout() routine. I have a number of things to find this button but it does 
not seem I can find it. This portion of the web page may also reside within 
a frame but when I save all the frames source to a temp file this part of 
the source never shows up.

So I am at a loss to figure out how to get this to work and would appreciate 
any help I can get. I am not very familiar with trying to accomplish this 
type of work so samples code on how to get this to work would certainly be 
appreciated.

Thank you for helping if you can.

Mike 
Re: Help with TCppWebBrowser - Find and click a button
#460
Author: Remy Lebeau (TeamB)
Date: Fri, 05 Jan 2007 21:41
121 lines
4593 bytes
"mike" <mfbrowne@earthlink.nnet> wrote in message
news:459ebec4$1@newsgroups.borland.com...

> I would like to programmably be able to click a button on a web
> page loaded into a TCppWebBrowser.

You need to use the DOM intefaces for that.  If the button has a name,
then you can use the IHTMLDocument2::all collection to find it
quickly.  Otherwise, you have to drill down the parent/child hierarchy
manually.  Either way, once you locate the button, you can obtain its
IHTMLElement interface and then call its click() method.

> The web page has a Tree View, I believe, on the left side and the
> button is located within this view.

There is no TreeView control in HTML.

> If I view the source from within the browser the source looks like
below:

The only non-form button I see in that code is named "secure_exit".  I
am assuming that the one you want.  The code to access it would look
like this:

    #include <mshtml.h>
    #include <utilcls.h>

    if( CppWebBrowser1->Document )
    {
        TComInterface<IHTMLDocument2> Doc;
        CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc);

        if( Doc )
        {
            TComInterface<IHTMLElementCollection> All;
            Doc->get_all(&All);

            if( All )
            {
                TComInterface<IDispatch> Disp;
                All->item(TVariant(WideString("secure_exit")),
TVariant(0), &Disp);

                if( Disp )
                {
                    TComInterface<IHTMLElement> Element;
                    Disp->QueryInterface(IID_IHTMLElement,
(LPVOID*)&Element);

                    if( Element )
                        Element->click();
                }
            }
        }
    }

> This portion of the web page may also reside within a frame

That makes a big difference.  You need the IHTMLDocument2 interface
for the specific frame that you are going to search.  Starting with
the top-level Document, you can use its frames collection to access
the IHTMLWindow2 interface of each frame.  You can then use the
IHTMLWindow2::document property to get the IHTMLDocument2 interface.
For example:

    #include <mshtml.h>
    #include <utilcls.h>

    if( CppWebBrowser1->Document )
    {
        TComInterface<IHTMLDocument2> Doc;
        CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc);

        if( Doc )
        {
            TComInterface<IHTMLFramesCollection2> Frames;
            Doc->get_frames(&Frames);

            if( Frames )
            {
                TVariant IndexOrName = WideString("frame name");
                // or:
                // TVariant IndexOrName = SomeIndex;

                TVariant Frame;
                Frames->item(&IndexOrName, &Frame);

                if( (V_VT(&Frame) == VT_DISPATCH) &&
(V_DISPATCH(&Frame) != NULL) )
                {
                    TComInterface<IHTMLWindow2> Window;

V_DISPATCH(&Frame)->QueryInterface(IID_IHTMLWindow2,
(LPVOID*)&Window);

                    if( Window )
                    {
                        TComInterface<IHTMLDocument2> FrameDoc;
                        Window->get_document(&FrameDoc);

                        if( FrameDoc )
                        {
                            // use FrameDoc as needed ...
                        }
                    }
                }
            }
        }
    }

Refer to the following documentation for more information:

    Interfaces and Scripting Objects

http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/interface.asp


Gambit
Re: Help with TCppWebBrowser - Find and click a button
#461
Author: mike
Date: Fri, 05 Jan 2007 22:48
132 lines
5064 bytes
Thank you Remy for responding to my question and problem.

I will study the code over the weekend and see if I can get this to work 
properly.

Mike

"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:459ec5bf$1@newsgroups.borland.com...
>
> "mike" <mfbrowne@earthlink.nnet> wrote in message
> news:459ebec4$1@newsgroups.borland.com...
>
>> I would like to programmably be able to click a button on a web
>> page loaded into a TCppWebBrowser.
>
> You need to use the DOM intefaces for that.  If the button has a name,
> then you can use the IHTMLDocument2::all collection to find it
> quickly.  Otherwise, you have to drill down the parent/child hierarchy
> manually.  Either way, once you locate the button, you can obtain its
> IHTMLElement interface and then call its click() method.
>
>> The web page has a Tree View, I believe, on the left side and the
>> button is located within this view.
>
> There is no TreeView control in HTML.
>
>> If I view the source from within the browser the source looks like
> below:
>
> The only non-form button I see in that code is named "secure_exit".  I
> am assuming that the one you want.  The code to access it would look
> like this:
>
>    #include <mshtml.h>
>    #include <utilcls.h>
>
>    if( CppWebBrowser1->Document )
>    {
>        TComInterface<IHTMLDocument2> Doc;
>        CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,
> (LPVOID*)&Doc);
>
>        if( Doc )
>        {
>            TComInterface<IHTMLElementCollection> All;
>            Doc->get_all(&All);
>
>            if( All )
>            {
>                TComInterface<IDispatch> Disp;
>                All->item(TVariant(WideString("secure_exit")),
> TVariant(0), &Disp);
>
>                if( Disp )
>                {
>                    TComInterface<IHTMLElement> Element;
>                    Disp->QueryInterface(IID_IHTMLElement,
> (LPVOID*)&Element);
>
>                    if( Element )
>                        Element->click();
>                }
>            }
>        }
>    }
>
>> This portion of the web page may also reside within a frame
>
> That makes a big difference.  You need the IHTMLDocument2 interface
> for the specific frame that you are going to search.  Starting with
> the top-level Document, you can use its frames collection to access
> the IHTMLWindow2 interface of each frame.  You can then use the
> IHTMLWindow2::document property to get the IHTMLDocument2 interface.
> For example:
>
>    #include <mshtml.h>
>    #include <utilcls.h>
>
>    if( CppWebBrowser1->Document )
>    {
>        TComInterface<IHTMLDocument2> Doc;
>        CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,
> (LPVOID*)&Doc);
>
>        if( Doc )
>        {
>            TComInterface<IHTMLFramesCollection2> Frames;
>            Doc->get_frames(&Frames);
>
>            if( Frames )
>            {
>                TVariant IndexOrName = WideString("frame name");
>                // or:
>                // TVariant IndexOrName = SomeIndex;
>
>                TVariant Frame;
>                Frames->item(&IndexOrName, &Frame);
>
>                if( (V_VT(&Frame) == VT_DISPATCH) &&
> (V_DISPATCH(&Frame) != NULL) )
>                {
>                    TComInterface<IHTMLWindow2> Window;
>
> V_DISPATCH(&Frame)->QueryInterface(IID_IHTMLWindow2,
> (LPVOID*)&Window);
>
>                    if( Window )
>                    {
>                        TComInterface<IHTMLDocument2> FrameDoc;
>                        Window->get_document(&FrameDoc);
>
>                        if( FrameDoc )
>                        {
>                            // use FrameDoc as needed ...
>                        }
>                    }
>                }
>            }
>        }
>    }
>
> Refer to the following documentation for more information:
>
>    Interfaces and Scripting Objects
>
> http://msdn.microsoft.com/workshop/browser/mshtml/reference/ifaces/interface.asp
>
>
> Gambit
>
> 
Re: Help with TCppWebBrowser - Find and click a button
#462
Author: mike
Date: Sat, 06 Jan 2007 15:12
35 lines
2323 bytes
Remy,

Again thank you for responding to me on this problem.

I tried the code you supplied and have the following results:

For the first code sample you supplied, the button was not found. Now 
because I suspected there might be frames, and did find 2 frames located on 
the web page. So I tried the second code sample and added the

           TComInterface<IHTMLElementCollection> All;

for the framedoc reteived. However the button again was not found. I tried 
this on both frame 0 and frame 1.

I then decided to get the source of the frames and save them to a file where 
I could see what was actually within the framedoc. Upon this review I did 
not see the button source in either frame doc. If I do a view source over 
the area that does contain the button I do see it in notepad.

So I am totally confused on where this button definition could be within the 
web page, and I am not sure how to find this information out.

I have reviewed the documentation you pointed me to but I am not sure what I 
am looking for to help me out.

I can supply you with the web page navigation to see the web page yourself 
if this will help out.

So I am at a loss and I am hoping you can point me in the right direction to 
help me.

Thanks

Mike 
Re: Help with TCppWebBrowser - Find and click a button
#463
Author: Hans Galema
Date: Sat, 06 Jan 2007 22:22
52 lines
2414 bytes
mike wrote:

> For the first code sample you supplied, the button was not found.

How did you check that? The provided code is ok. You could add a
Showmessage to indicate that it found the button:


                     if( Element )
			{
                         Element->click();
			
			ShowMessage ( "Element->click();");
			}

> Now 
> because I suspected there might be frames,

There are no frames in the page you posted.

At first I thought that there were two buttons in that page. Well
there are but the second one is commented out. Please clean up
a page before you post it.

So it is about this button:

       <p><input TYPE="button" NAME="secure_exit" VALUE=" Finished "
onClick="logout();">

The button is found and clicked with the provided code. Didn't
you see anyting happen? I did not either but that is because
the nesasary javasript function logout() failed.

Then i made the following function:

<script language=javascript>
function logout()
  {
  var url = "http://www.borland.com";
  var widgeds = 'toolbar=0,status=0,width=200,height=150';
  window.open(url,"",widgeds);
  }
</script>

Now all functioned.

 > I can supply you with the web page navigation to see the web page yourself
 > if this will help out.

Why did not you post the exacturl with your first post?

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#464
Author: mike
Date: Sat, 06 Jan 2007 23:04
109 lines
4110 bytes
Hans,

Thank you for responding, I appreciate it.

Please see below for my answers on your questions.




"Hans Galema" <notused@notused.nl> wrote in message 
news:45a02189$1@newsgroups.borland.com...
> mike wrote:
>
>> For the first code sample you supplied, the button was not found.
>
> How did you check that? The provided code is ok. You could add a
> Showmessage to indicate that it found the button:
>
>
>                     if( Element )
> {
>                         Element->click();
>
> ShowMessage ( "Element->click();");
> }
>

I did two things to verify the button was not found. The first thing is I 
traced the test application. The code to descover the element and click it 
was never executed. I also did put a show message in and ran the application 
and it again did not find the button.



>> Now because I suspected there might be frames,
>
> There are no frames in the page you posted.
>
The actual page I posted was from a show source of the actual page during an 
internet session on the page. If I do a show source on different parts of 
the internet window shown then I see different sources for the page. I did 
not post all the pages as I would have made the original post rather long. 
In my test application I have inserted a test for frames and it does that 
there are 2 frames.



> At first I thought that there were two buttons in that page. Well
> there are but the second one is commented out. Please clean up
> a page before you post it.
>

Well as I did not write this web page and it is from a server on the net I 
wanted to show the page as I was seeing it from my test application. This is 
why I did not clean it up.


> So it is about this button:

Yes

>
>       <p><input TYPE="button" NAME="secure_exit" VALUE=" Finished "
> onClick="logout();">
>
> The button is found and clicked with the provided code. Didn't
> you see anyting happen? I did not either but that is because
> the nesasary javasript function logout() failed.

Ther function logout() should be executed from the source server. I have no 
control over that.


>
> Then i made the following function:
>
> <script language=javascript>
> function logout()
>  {
>  var url = "http://www.borland.com";
>  var widgeds = 'toolbar=0,status=0,width=200,height=150';
>  window.open(url,"",widgeds);
>  }
> </script>
>
> Now all functioned.
>
> > I can supply you with the web page navigation to see the web page 
> > yourself
> > if this will help out.
>
> Why did not you post the exacturl with your first post?

Well I suppose I should have posted it in the first post. I am posting it 
now.

http://rpa.tblc.org/rpa/webauth.exe?lb=PALM

Borrower Id = 25913000705454

It will be the next page the button I am trying to access will be on. When I 
try and find this button through my test application I never see it.

I hope you can assist me in this as I really do not understand why I cannot 
get to this button.

Thanks very much.

Mike 
Re: Help with TCppWebBrowser - Find and click a button
#465
Author: Hans Galema
Date: Sun, 07 Jan 2007 09:50
25 lines
1974 bytes
mike wrote:

> http://rpa.tblc.org/rpa/webauth.exe?lb=PALM

That is indeed a frameset after login.
/rpa/default/thetop.htm is the topframe and
/rpa/default/newauthlink.htm the bottomframe.

 From that bottomframe the leftframe is
http://rpa.tblc.org/rpa/default/newlinks.htm
and you posted the complete code of that page.

You could have provided that info to begin with!

If you navigate directly to that page then the list
of authorized links is empty. But the Finished button
is there and Remy's code works. After using it a trouble-
shooting page displays.

Now what is the problem?

As you see: I did not try the code looping through all the
frames yet but if that is what you need I could do that too.

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#467
Author: Hans Galema
Date: Sun, 07 Jan 2007 10:02
12 lines
1482 bytes
mike wrote:

> Ther function logout() should be executed from the source server. I have no 
> control over that.

Javascript is interpreted on the clientside. In your browser.

The logout() function is found in:
http://rpa.tblc.org/rpa/default/newAuthlib.js
which is included in the page we are speaking about.

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#466
Author: Hans Galema
Date: Sun, 07 Jan 2007 10:04
16 lines
1720 bytes
Hans Galema wrote:

>> http://rpa.tblc.org/rpa/webauth.exe?lb=PALM
> 
> That is indeed a frameset after login.
> /rpa/default/thetop.htm is the topframe and
> /rpa/default/newauthlink.htm the bottomframe.
> 
>  From that bottomframe the leftframe is
> http://rpa.tblc.org/rpa/default/newlinks.htm

So there are two framesets. I think that the code that Remy
posted only takes one frameset in account. You have to
make that code recursive so that it finds the second frameset too.

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#468
Author: mike
Date: Sun, 07 Jan 2007 14:07
28 lines
2062 bytes
Hans,

Thank you very much for having a look at this for me, and now I will try and 
understand what you have said to me.

I did modify Remy's code sample to take into account the 2 frames that I 
found, but when I did I still did not find the button within either frameset 
I located. So I must be doing something incorrect. I will relook at the code 
sample and see if I can determine what I may be doing wrong and then post it 
here for help.

Again thanks so much for assisting me.

Mike
"Hans Galema" <notused@notused.nl> wrote in message 
news:45a0c58e$1@newsgroups.borland.com...
> mike wrote:
>
>> Ther function logout() should be executed from the source server. I have 
>> no control over that.
>
> Javascript is interpreted on the clientside. In your browser.
>
> The logout() function is found in:
> http://rpa.tblc.org/rpa/default/newAuthlib.js
> which is included in the page we are speaking about.
>
> Hans. 
Re: Help with TCppWebBrowser - Find and click a button
#469
Author: mike
Date: Sun, 07 Jan 2007 14:40
83 lines
3944 bytes
Hans,

Well I am clearly not understanding why the button is not found. I am using 
the following code and I thought the button would have been found as I am 
taking into account the 2 frames, I think.

Can you point out what I am not understanding or where I have gone wrong.

Thanks very much

Mike

    if( CppWebBrowser1->Document )
    {
        TComInterface<IHTMLDocument2> Doc;
        CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&Doc);

        if( Doc )
        {
            TComInterface<IHTMLFramesCollection2> Frames;
            Doc->get_frames(&Frames);

            if( Frames )
            {

                long cnt = 0;
                Frames->get_length(&cnt);


                for(long l = 0; l < cnt; ++l)
                {
//                TVariant IndexOrName = WideString("frame name");
// or:
                    TVariant IndexOrName = l;
                    TVariant Frame;

                    Frames->item(&IndexOrName, &Frame);

                    if( (V_VT(&Frame) == VT_DISPATCH) && (V_DISPATCH(&Frame) 
!= NULL) )
                    {
                         TComInterface<IHTMLWindow2> Window;

                         V_DISPATCH(&Frame)->QueryInterface(IID_IHTMLWindow2,(LPVOID*)&Window);

                         if( Window )
                         {
                             TComInterface<IHTMLDocument2> FrameDoc;
                             Window->get_document(&FrameDoc);

                             if( FrameDoc )
                             {
                            // use FrameDoc as needed ...
                                TComInterface<IHTMLElementCollection> All;
                                FrameDoc->get_all(&All);

                                if( All )
                                {
                                    TComInterface<IDispatch> Disp;
                                    All->item(TVariant(WideString("secure_exit")),TVariant(0), 
&Disp);

                                    if( Disp )
                                    {
                                        TComInterface<IHTMLElement> Element;
                                        Disp->QueryInterface(IID_IHTMLElement,(LPVOID*)&Element);

                                        if( Element )
                                        {
                                            ShowMessage ( 
"Element->click();");
                                            Element->click();
                                            break ;
                                         }
                                     }
                                }
                             }
                         }
                    }
                }
            }
        }
    }
Re: Help with TCppWebBrowser - Find and click a button
#470
Author: Michael Harris
Date: Sun, 07 Jan 2007 16:22
21 lines
1728 bytes
if you add the 'get_source' method mentioned earlier, you will see why the 
code failes.
 if( FrameDoc )
  {
ShowMessage(Get_Source(FrameDoc));
       // use FrameDoc as needed ...

--
Michael


"mike"wrote in message
> Hans,
>
> Well I am clearly not understanding why the button is not found. I am 
> using the following code and I thought the button would have been found as 
> I am taking into account the 2 frames, I think.
>
> Can you point out what I am not understanding or where I have gone wrong.
>
> 
Re: Help with TCppWebBrowser - Find and click a button
#471
Author: mike
Date: Sun, 07 Jan 2007 17:29
35 lines
2102 bytes
Michael

Thank you for responding, however I get an undefined function when inserting 
this.

Where is the function defined or how to get the actual source of the frame.

Thanks

Mike
"Michael Harris" <techonos-NO-SPAM-@attbi.com> wrote in message 
news:45a11e90$1@newsgroups.borland.com...
> if you add the 'get_source' method mentioned earlier, you will see why the 
> code failes.
> if( FrameDoc )
>  {
> ShowMessage(Get_Source(FrameDoc));
>       // use FrameDoc as needed ...
>
> --
> Michael
>
>
> "mike"wrote in message
>> Hans,
>>
>> Well I am clearly not understanding why the button is not found. I am 
>> using the following code and I thought the button would have been found 
>> as I am taking into account the 2 frames, I think.
>>
>> Can you point out what I am not understanding or where I have gone wrong.
>>
>>
>
> 
Re: Help with TCppWebBrowser - Find and click a button
#472
Author: Hans Galema
Date: Sun, 07 Jan 2007 17:43
22 lines
1746 bytes
mike wrote:

Until now I did not try your code but looking at it the following
comes to mind:

>                 long cnt = 0;
>                 Frames->get_length(&cnt);

What is the value of count?

	ShowMessage ( "cnt: " + IntToStr ( (int)cnt ) );

>                 for(long l = 0; l < cnt; ++l)
>                 {
>                     TVariant IndexOrName = l;

Is that character L ? or digit 1 ?

Please do never use variablenames with only one character. Especially
do never use an l. It looks to much a 1.

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#473
Author: mike
Date: Sun, 07 Jan 2007 18:04
33 lines
2018 bytes
Hans,

Sorry for the one character variable name. It is  L not a digit 1.

The reported count for the number of frames is 2.

Thanks for continuing to assist me in this.

Mike
"Hans Galema" <notused@notused.nl> wrote in message 
news:45a131a1$1@newsgroups.borland.com...
> mike wrote:
>
> Until now I did not try your code but looking at it the following
> comes to mind:
>
>>                 long cnt = 0;
>>                 Frames->get_length(&cnt);
>
> What is the value of count?
>
> ShowMessage ( "cnt: " + IntToStr ( (int)cnt ) );
>
>>                 for(long l = 0; l < cnt; ++l)
>>                 {
>>                     TVariant IndexOrName = l;
>
> Is that character L ? or digit 1 ?
>
> Please do never use variablenames with only one character. Especially
> do never use an l. It looks to much a 1.
>
> Hans. 
Re: Help with TCppWebBrowser - Find and click a button
#477
Author: Hans Galema
Date: Mon, 08 Jan 2007 11:09
14 lines
1730 bytes
mike wrote:

> Well I am clearly not understanding why the button is not found. I am using 
> the following code and I thought the button would have been found as I am 
> taking into account the 2 frames, I think.

Yes. But the second frame contains a doc ( a html-page ) that contains
again a frameset. So you should make the code recursive.

Every TComInterface<IHTMLDocument2> Doc; you should investigate for
TComInterface<IHTMLFramesCollection2> Frames;. Then every frame from
that collection contains a Doc again.

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#479
Author: Hans Galema
Date: Mon, 08 Jan 2007 12:14
123 lines
4067 bytes
Hans Galema wrote:

> ... So you should make the code recursive.

In order to do that start at the begin. I first took Remy's code to
click a button if the webbrowsercontrol contains a document without
a frameset. I first made a function of it. ( I keep functions concerning
T(Cpp)WebBrowser in a separate file which is easy added to a project
with the browsercomponent).

To click a certain button then only a few lines are needed. In your
case:

  AnsiString ButtonName = "secure_exit" ;

  if ( ! WBClickButton ( WebBrowser1, ButtonName) )
     ShowMessage ( ButtonName + "\n\nbutton not clicked");

Here is the function:

bool __fastcall WBClickButton ( TWebBrowser *WebBrowser, AnsiString ButtonName )
{
	bool done = false;

	if( WebBrowser->Document )
		{
		TComInterface<IHTMLDocument2> Doc;
		WebBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&Doc);

		if( Doc )
		{
			TComInterface<IHTMLElementCollection> All;
			Doc->get_all(&All);

			if( All )
			{
				TComInterface<IDispatch> Disp;
				All->item(TVariant(WideString(ButtonName)), TVariant(0), &Disp);

				if( Disp )
				{
					TComInterface<IHTMLElement> Element;
					Disp->QueryInterface(IID_IHTMLElement, (LPVOID*)&Element);

					if( Element )
					   {
						Element->click();

						done = true;
					   }

				}
			}
		}
	}

return done;
}

Then realising that traversing frames we need a function that finds
a button in a IHTMLDocument2 I made another version which puts all
the code in two functions:

bool __fastcall WBClickButton2 ( TWebBrowser *WebBrowser, AnsiString ButtonName )
{
	bool clicked = false;

	if( WebBrowser->Document )
		{
		TComInterface<IHTMLDocument2> Doc;
		WebBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&Doc);

		if( Doc )
		{
		 clicked = DocClickButton2 ( Doc, ButtonName );  // see code below
		}
	}

return clicked;
}


bool __fastcall DocClickButton2 ( TComInterface<IHTMLDocument2> Doc, AnsiString ButtonName )
{
	bool clicked = false;

	TComInterface<IHTMLElementCollection> All;
	Doc->get_all(&All);

	if( All )
		{
		TComInterface<IDispatch> Disp;

		All->item(TVariant(WideString(ButtonName)), TVariant(0), &Disp);

		if( Disp )
			{
			TComInterface<IHTMLElement> Element;
			Disp->QueryInterface(IID_IHTMLElement, (LPVOID*)&Element);

			if( Element )
				{
				Element->click();

				clicked = true;
				}
			}
		}

	return clicked;
}

  AnsiString ButtonName = "secure_exit" ;

  if ( ! WBClickButton2 ( WebBrowser1, ButtonName) )
     ShowMessage ( ButtonName + "\n\nbutton not clicked");

Now we can use DocClickButton2() also on the documents for every frame when
traversing Frames.

That has to be done yet.

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#480
Author: Hans Galema
Date: Mon, 08 Jan 2007 13:00
143 lines
4136 bytes
Hans Galema wrote:

> That has to be done yet.

OK. I got it working.

void __fastcall TForm4::SpeedButton10Click(TObject *Sender)
{
	AnsiString URL = "http://rpa.tblc.org/rpa/webauth.exe?lb=PALM";

	WebBrowser1->Navigate ( URL );

	// You have to manually fill in BorrowerID and click the
	// Login button;
         // After that click SpeedButton13
}


void __fastcall TForm4::SpeedButton13Click(TObject *Sender)
{
   AnsiString ButtonName = "secure_exit";

   if ( ! WBClickButton3 ( WebBrowser1, ButtonName ) )
	ShowMessage ( ButtonName + "\n\nbutton not clicked");
}

The code:

static bool __fastcall DocClickButton2 ( TComInterface<IHTMLDocument2> Doc, AnsiString ButtonName )
{
	bool clicked = false;

	TComInterface<IHTMLElementCollection> All;
	Doc->get_all(&All);

	if( All )
		{
		TComInterface<IDispatch> Disp;

		All->item(TVariant(WideString(ButtonName)), TVariant(0), &Disp);

		if( Disp )
			{
			TComInterface<IHTMLElement> Element;
			Disp->QueryInterface(IID_IHTMLElement, (LPVOID*)&Element);

			if( Element )
				{
				Element->click();

				clicked = true;
				}
			}
		}

	return clicked;
}


int level = 0; // for testing purposes

static bool __fastcall DocClickButton3 ( IHTMLDocument2 *Doc, AnsiString ButtonName )
{
  level++;

	bool clicked = false;

	long cnt = 0;

	TComInterface<IHTMLFramesCollection2> Frames;

	Doc->get_frames(&Frames);

	if( Frames )
		{
		Frames->get_length(&cnt);

		ShowMessage ( "level: " + IntToStr ( level ) + "\n\ncnt: " + IntToStr ( (int)cnt ) );
		}

	if ( cnt > 0 )
		{
		for(long l = 0; l < cnt; ++l)
			{
//          TVariant IndexOrName = WideString("frame name");
// or:
			TVariant IndexOrName = l;
			TVariant Frame;

			Frames->item(&IndexOrName, &Frame);

			if( (V_VT(&Frame) == VT_DISPATCH) && (V_DISPATCH(&Frame) != NULL) )
				{
				TComInterface<IHTMLWindow2> Window;

				V_DISPATCH(&Frame)->QueryInterface(IID_IHTMLWindow2,(LPVOID*)&Window);

				if( Window )
					{
					TComInterface<IHTMLDocument2> FrameDoc;
					Window->get_document(&FrameDoc);

					if( FrameDoc )
						{
						// use FrameDoc as needed ...

						clicked = DocClickButton3 ( FrameDoc, ButtonName );

						if ( clicked )
							break;
						}
					}
				}
			}
		}
	else
		clicked = DocClickButton2 ( Doc, ButtonName );

  level --;

   return clicked;
}


bool __fastcall WBClickButton3 ( TWebBrowser *WebBrowser, AnsiString ButtonName )
{
	bool clicked = false;

	if( WebBrowser->Document )
		{
		TComInterface<IHTMLDocument2> Doc;
		WebBrowser->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&Doc);

		if( Doc )
			{
			clicked = DocClickButton3 ( Doc, ButtonName );
			}
		}

	return clicked;
}

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#481
Author: Hans Galema
Date: Mon, 08 Jan 2007 13:14
14 lines
1711 bytes
Hans Galema wrote:

>         for(long l = 0; l < cnt; ++l)
>             {
> //          TVariant IndexOrName = WideString("frame name");
> // or:
>             TVariant IndexOrName = l;

Oh that is terrible! I left the L!

And JD: I left the for too! To honour the original poster. You know
that I will never use a for myself.

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#485
Author: mike
Date: Mon, 08 Jan 2007 14:46
160 lines
4514 bytes
Hans,

I have tested the code on my side and it does work absolutely fantastic.

I cannot thank you enough for all the help you have provided. I know you 
have spent your own personal time in looking at this for me and I appreciate 
it very much.

Again, thank you and have a wonderful day.

Mike
"Hans Galema" <notused@notused.nl> wrote in message 
news:45a240c7@newsgroups.borland.com...
> Hans Galema wrote:
>
>> That has to be done yet.
>
> OK. I got it working.
>
> void __fastcall TForm4::SpeedButton10Click(TObject *Sender)
> {
> AnsiString URL = "http://rpa.tblc.org/rpa/webauth.exe?lb=PALM";
>
> WebBrowser1->Navigate ( URL );
>
> // You have to manually fill in BorrowerID and click the
> // Login button;
>         // After that click SpeedButton13
> }
>
>
> void __fastcall TForm4::SpeedButton13Click(TObject *Sender)
> {
>   AnsiString ButtonName = "secure_exit";
>
>   if ( ! WBClickButton3 ( WebBrowser1, ButtonName ) )
> ShowMessage ( ButtonName + "\n\nbutton not clicked");
> }
>
> The code:
>
> static bool __fastcall DocClickButton2 ( TComInterface<IHTMLDocument2> 
> Doc, AnsiString ButtonName )
> {
> bool clicked = false;
>
> TComInterface<IHTMLElementCollection> All;
> Doc->get_all(&All);
>
> if( All )
> {
> TComInterface<IDispatch> Disp;
>
> All->item(TVariant(WideString(ButtonName)), TVariant(0), &Disp);
>
> if( Disp )
> {
> TComInterface<IHTMLElement> Element;
> Disp->QueryInterface(IID_IHTMLElement, (LPVOID*)&Element);
>
> if( Element )
> {
> Element->click();
>
> clicked = true;
> }
> }
> }
>
> return clicked;
> }
>
>
> int level = 0; // for testing purposes
>
> static bool __fastcall DocClickButton3 ( IHTMLDocument2 *Doc, AnsiString 
> ButtonName )
> {
>  level++;
>
> bool clicked = false;
>
> long cnt = 0;
>
> TComInterface<IHTMLFramesCollection2> Frames;
>
> Doc->get_frames(&Frames);
>
> if( Frames )
> {
> Frames->get_length(&cnt);
>
> ShowMessage ( "level: " + IntToStr ( level ) + "\n\ncnt: " + IntToStr ( 
> (int)cnt ) );
> }
>
> if ( cnt > 0 )
> {
> for(long l = 0; l < cnt; ++l)
> {
> //          TVariant IndexOrName = WideString("frame name");
> // or:
> TVariant IndexOrName = l;
> TVariant Frame;
>
> Frames->item(&IndexOrName, &Frame);
>
> if( (V_VT(&Frame) == VT_DISPATCH) && (V_DISPATCH(&Frame) != NULL) )
> {
> TComInterface<IHTMLWindow2> Window;
>
> V_DISPATCH(&Frame)->QueryInterface(IID_IHTMLWindow2,(LPVOID*)&Window);
>
> if( Window )
> {
> TComInterface<IHTMLDocument2> FrameDoc;
> Window->get_document(&FrameDoc);
>
> if( FrameDoc )
> {
> // use FrameDoc as needed ...
>
> clicked = DocClickButton3 ( FrameDoc, ButtonName );
>
> if ( clicked )
> break;
> }
> }
> }
> }
> }
> else
> clicked = DocClickButton2 ( Doc, ButtonName );
>
>  level --;
>
>   return clicked;
> }
>
>
> bool __fastcall WBClickButton3 ( TWebBrowser *WebBrowser, AnsiString 
> ButtonName )
> {
> bool clicked = false;
>
> if( WebBrowser->Document )
> {
> TComInterface<IHTMLDocument2> Doc;
> WebBrowser->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&Doc);
>
> if( Doc )
> {
> clicked = DocClickButton3 ( Doc, ButtonName );
> }
> }
>
> return clicked;
> }
>
> Hans. 
Re: Help with TCppWebBrowser - Find and click a button
#387
Author: Michael Harris
Date: Mon, 08 Jan 2007 19:05
65 lines
3041 bytes
get source is similar to below.

As Hans pointed out. the html being parser in your iter. function is not 
finding the actual html you are seeing on the page.
Hans also provided source to iter the frames in the various framesets.

--

Michael

AnsiString GetSource ( TCppWebBrowser *CppWebBrowser )
{
  AnsiString Source = "";

  IHTMLDocument2 *htm = NULL;     // #include <mshtml.h>

  if(CppWebBrowser->Document
     && 
SUCCEEDED(CppWebBrowser->Document->QueryInterface(IID_IHTMLDocument2, 
(LPVOID*)&htm))
     )
    {
    IPersistStreamInit *spPsi = NULL;    // ocidl.h

    if(SUCCEEDED(htm->QueryInterface(IID_IPersistStreamInit, 
(LPVOID*)&spPsi)) &&spPsi)
       {
        IStream *spStream = NULL;            // objidl.h
            OleCheck(CreateStreamOnHGlobal(NULL, true, &spStream));
            if(spStream)
            {
                __int64 nSize = 0;
                STATSTG ss;
                LARGE_INTEGER nMove;
                nMove.QuadPart = 0;
                OleCheck(spPsi->Save(spStream, true));
                OleCheck(spStream->Seek(nMove, STREAM_SEEK_SET, 
(ULARGE_INTEGER*)&nSize));
                OleCheck(spStream->Stat(&ss, STATFLAG_NONAME));
                nSize = ss.cbSize.QuadPart;

                Source.SetLength(nSize);
                OleCheck(spStream->Read((void *)Source.data(), nSize, 
(ULONG*)&nSize));
                OleCheck(spStream->Release());
            }

         spPsi->Release();
        }
    htm->Release();
    }

  return Source;
}

"mike"  wrote in message

>>> Can you point out what I am not understanding or where I have gone 
>>> wrong.
>>>
>>>
>>
>>
>
> 
Re: Help with TCppWebBrowser - Find and click a button
#388
Author: Michael Harris
Date: Mon, 08 Jan 2007 19:20
63 lines
3326 bytes
If you are interested in seeing what I am talking about.
***** add abutton to your form.
void __fastcall TForm1::Button9Click(TObject *Sender)
{
   AnsiString ButtonName = "secure_exit";

   if ( ! WBClickButton3 ( FBrowser, ButtonName ) )
 ShowMessage ( ButtonName + "\n\nbutton not clicked");
}

******* alter DocClickButton3
if( FrameDoc )
      {
      // ****** add this line to view the actual source
      ShowMessage(GetDocSource ( (_id_IHTMLDocument2) FrameDoc ));
      clicked = DocClickButton3 ( FrameDoc, ButtonName );

      if ( clicked )
       break;
      }

*******change the get source method as below.
typedef CComPtr<IHTMLDocument2> _id_IHTMLDocument2;  //  or tcominterface
typedef CComPtr<IPersistStreamInit> _id_IPersistStreamInit;

AnsiString GetDocSource ( _id_IHTMLDocument2 htm )
{
  AnsiString Source = "";

  _id_IPersistStreamInit spPsi = NULL;    // ocidl.h

    if(SUCCEEDED(htm->QueryInterface(IID_IPersistStreamInit, 
(LPVOID*)&spPsi)) &&spPsi)
       {
        IStream *spStream = NULL;            // objidl.h
            OleCheck(CreateStreamOnHGlobal(NULL, true, &spStream));
            if(spStream)
            {
                __int64 nSize = 0;
                STATSTG ss;
                LARGE_INTEGER nMove;
                nMove.QuadPart = 0;
                OleCheck(spPsi->Save(spStream, true));
                OleCheck(spStream->Seek(nMove, STREAM_SEEK_SET, 
(ULARGE_INTEGER*)&nSize));
                OleCheck(spStream->Stat(&ss, STATFLAG_NONAME));
                nSize = ss.cbSize.QuadPart;

                Source.SetLength(nSize);
                OleCheck(spStream->Read((void *)Source.data(), nSize, 
(ULONG*)&nSize));
                OleCheck(spStream->Release());
            }
        }
  return Source;
}


as Han's Iter functions enumerate the frames, the html source should show.

--
Michael
Re: Help with TCppWebBrowser - Find and click a button
#389
Author: Hans Galema
Date: Tue, 09 Jan 2007 08:51
9 lines
1632 bytes
Michael Harris wrote:

> as Han's Iter functions enumerate the frames, the html source should show.

Indeed.

Nice to see that you experimented with the code to extract all the sources.

Hans.
Re: Help with TCppWebBrowser - Find and click a button
#390
Author: Remy Lebeau (TeamB)
Date: Tue, 09 Jan 2007 19:19
40 lines
2340 bytes
"mike" <mfbrowne@earthlink.nnet> wrote in message
news:459fbc29$2@newsgroups.borland.com...

> Now because I suspected there might be frames, and did find 2 frames
> located on the web page. So I tried the second code sample and added
the
>
>            TComInterface<IHTMLElementCollection> All;
>
> for the framedoc reteived. However the button again was not found. I
> tried this on both frame 0 and frame 1.

Then you are likely still not looking in the right page for the
button.  Please show the exact starting URL that you are trying to
access the button from.

> I then decided to get the source of the frames and save them to a
file
> where I could see what was actually within the framedoc. Upon this
> review I did not see the button source in either frame doc.

What exactly did you see?  Are there other frames that you are not
accounting for yet?

> If I do a view source over the area that does contain the button I
do see it in notepad.

Then all the more reason that you are not handling the frames 100%
correctly yet.

> So I am totally confused on where this button definition could be
> within the web page, and I am not sure how to find this information
out.

Then again, please show the original URL so someone else can look at
it.


Gambit
Re: Help with TCppWebBrowser - Find and click a button
#391
Author: Remy Lebeau (TeamB)
Date: Tue, 09 Jan 2007 19:24
16 lines
1629 bytes
"mike" <mfbrowne@earthlink.nnet> wrote in message
news:45a02ab5$1@newsgroups.borland.com...

> In my test application I have inserted a test for frames and it does
that
> there are 2 frames.

The top-level page has 2 frames in it, yes.  But the bottom frame has
*2 MORE* frames inside of it.  The button you seek is inside the left
inner frame.  That is what you have not taken into account properly
yet.  Which is why your code can't find it yet.  You are not going
deep enough.


Gambit
Re: Help with TCppWebBrowser - Find and click a button
#392
Author: Remy Lebeau (TeamB)
Date: Tue, 09 Jan 2007 19:25
12 lines
1521 bytes
"Hans Galema" <notused@notused.nl> wrote in message
news:45a0c612$1@newsgroups.borland.com...

> So there are two framesets. I think that the code that Remy
> posted only takes one frameset in account. You have to
> make that code recursive so that it finds the second frameset too.

It does not need to be recursive.


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads