Link to home
Start Free TrialLog in
Avatar of wildzero
wildzero

asked on

Webbrowser - Getting form elements in frames

Gidday there,

If I load a page and use
showmessage(IntToStr(WebBrowser.OleObject.Document.Forms.Length))
I get the number of forms on the page.
ie 1 or 2 ect

Now if that page has frames it always returns 0
How can I get the count of forms, or more importantly, how can I access a forms elements on a page with frames?
Like
WebBrowser.OleObject.document.forms.item(1).elements.item('myname').value:= 'Bob';

Cheers guys
Avatar of wildzero
wildzero

ASKER

Points upped
Avatar of Wayne Barron
If you had a link that I could help test on that would be helpful.
But anyway, take a look at this code.
And see if you can accomplish what you need with it.

How t0 interate through Frames
=====================================
procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to (WebBrowser1.OleObject.Document.frames.Length - 1) do
    if WebBrowser1.OleObject.Document.frames.item(i).document.queryCommandEnabled('Copy') then
      ShowMessage('copy command is enabled for frame no.' + IntToStr(i));
end;
======================================
Carrzkiss
Carrzkiss,

Gidday there
Let me throw up a site - while I do that

If thats how to interate though frames, how could I use that to fill out a form on a frame?
like, interate though frames to interate though forms ;)
Put up a site, and send the link over, I will take a look at it.
Also, if you have some sample code that you are using to do it.
Without Frames, Then please post that as well.
This way we are both working on the same track.

Carrzkiss
Code for filling in the forms, it what I meant to say
http://storeit-online.com/framestest/index.html

There is a url there of something I am trying to do, just like fillin the fields of say, the main page (name and url)
I tried using this

For i := 0 to WebBrowser.OleObject.Document.Forms.Length-1 do
  begin
    WebBrowser.OleObject.document.forms.item(i).elements.item('name').value:= 'Bob';
    WebBrowser.OleObject.document.forms.item(i).elements.item(url).value:= 'http://mysite.com';
  end;


Even just showmessage(IntToStr(WebBrowser.OleObject.Document.Forms.Length));
which normally returns more then 0
Hey there, manage to get any further?
Figured it out! Finally

procedure TForm1.Button5Click(Sender: TObject);
var
  g, f, k : integer;
  pDispatch : IDISPATCH;
  textarea: IHTMLTextAreaElement;
begin
  for f := 0 to WebBrowser1.OleObject.Document.frames.Length - 1 do
    begin
      showmessage(intToStr(WebBrowser1.OleObject.Document.frames.item(f).document.Forms.Length));
        for g := 0 to WebBrowser1.OleObject.Document.frames.item(f).document.Forms.Length-1 do
          begin
             for k := 0 to WebBrowser1.OleObject.Document.frames.item(f).document.forms.item(g).elements.length-1 do
              begin
                showmessage(WebBrowser1.OleObject.Document.frames.item(f).document.forms.item(g).elements.item(k).name);
                showmessage(WebBrowser1.OleObject.Document.frames.item(f).document.forms.item(g).elements.item(k).tagname);
                showmessage(WebBrowser1.OleObject.Document.frames.item(f).document.forms.item(g).elements.item(k).type);
                {Fill text field}
                WebBrowser1.OleObject.Document.frames.item(f).document.forms.item(g).elements.item('name').value := 'Bob';
                {Fill in a text area}
                pDispatch := WebBrowser1.OleObject.Document.frames.item(2).document.forms.item(0).elements.item('S1', 0);
                  try
                    OleCheck(pDispatch.QueryInterface(IID_IHTMLTextAreaElement, textarea));
                    textarea.Set_value('Hello');
                  except
                  end;
              end;
          end;
    end;
end;
Hello WildZero;

  I am so sorry that I have not gotten back with you.
It has been rather hectic here at my house over the weekend.
And I just took a break from doing anything on the computer.
(The life of being a single parent)
I am glad to see that you was able to figure it out with the
Code that I sent over.

Maybe I am just wore out, But I cannot get it to compile.
Getting a few errors. ( Maybe rest will help my brain ;-)  )

[Error] Unit1.pas(34): Undeclared identifier: 'IHTMLTextAreaElement'
[Error] Unit1.pas(51): Undeclared identifier: 'OleCheck'
[Error] Unit1.pas(51): Undeclared identifier: 'IID_IHTMLTextAreaElement'
&
[Error] Unit1.pas(52): Missing operator or semicolon
On this:                     textarea.Set_value('Hello');

Anyway.
Hope that you make something that makes you a lot of fortune.

Take Care
Carrzkiss
Your errros come from missing units in your uses.
Be aware that you WILL NOT be able to do this on frames that are not on your the same domain as
the frameset. obtaining info from cross-domain frames will be caught by IE's security measures.
MSHTML_TLB (MSHTML for some) and SHDocVw I think it is you need.

Eddie, you are right - and thats the thing I need to work nothing you can think of/suggest?
Uses MSHTML_TLB, SHDocVw, ComObj;

Thanks, good now I can test it out.
I can't get that code to work at all.
SOLUTION
Avatar of Wayne Barron
Wayne Barron
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
"WebBrowser1.OleObject.Document.frames.item(0).Document.forms.Item(0).Item(0).Value:='Bob';" will set the value of the first item on the first form of the first frame to 'Bob' provided that  such an item, form and frame exist, and provided that the item can be set.

Hope that helps you.

A.
Aqueath,
  It doesn't work on ;ater versions of IE6 because of cross-domain security.
I can't even get it to work when the frame is on my own domain.
Eddie,
According to Windows Update I have upgraded to the latest version of IE6, and this still works on my machine. I did however use the TWebBrowser component while testing this. Can you list the page you tried this on? And you IE6 version?

A.
A.

I got the code I posted to work fine but doesn't with cross domain things.
ie one frame on a different domain name.

Did you try that?
I am running IE6 on both machines
I tried it both ways and couldn't get it to work in either situation.

Here is the HTML I used:
<html>
  <head>
    <title>Untitled Document</title>
  </head>
  <frameset rows="14.3%,*,12.3%" frameborder="NO" border="1" framespacing="0">
    <frame src="gallery_2.html" name="topFrame" scrolling="NO" noresize frameborder="no" border="0">
      <frameset cols="27%,70%" frameborder="no" border="1" framespacing="0" marginwidth=0 marginheight=0>
        <frame src="gallery_3.html" name="mainFrame" scrolling="NO" noresize>
        <frame src="http://www.delphipages.com" name="dynamicframe" scrolling="yes" noresize frameborder="yes">
      </frameset>
    <frame src="gallery_4.html" name="topFrames" scrolling="NO" noresize frameborder="no">          
  </frameset>
</html>

The gallery_X.html pages are pages on my site.

If I change the delphipages.com url to another page on my site, it still doesn't work.

Of course I'm also trying this locally, too, (http://locahost/framsettest.html) and it doesn't work in either case.

With
WebBrowser1.OleObject.Document.frames.item(0).Document.forms.Item(0).Item(0).Value:='Bob';
I get Access Denied.

When I do this
  showmessage(IntToStr(WebBrowser1.OleObject.Document.frames.Length));
  for f := 0 to WebBrowser1.OleObject.Document.frames.Length - 1 do
    showmessage(intToStr(WebBrowser1.OleObject.Document.frames.item(f).document.Forms.Length));

I get the first message (2 for two forms)
But then I get a access denied message as well.....

Wildzero,
Can you tell me the url of the page you're trying to reach?

A.
I just tried this and got the same result.

Open up notepad and paste this

<html>
  <head>
    <title>Untitled Document</title>
  </head>
  <frameset rows="595,*,115" frameborder="NO" border="1" framespacing="0">
   <frame src="http://www.delphipages.com" name="dynamicframe" scrolling="yes" noresize frameborder="yes">       
      <frameset cols="51%,49%" frameborder="no" border="1" framespacing="0" marginwidth=0 marginheight=0>
        <frame name="mainFrame" scrolling="NO" noresize>
        <frame name="topFrame" scrolling="NO" noresize frameborder="no" border="0">
       
      </frameset>
    <frame name="topFrames" scrolling="NO" noresize frameborder="no">          
  </frameset>
</html>

Save as test.html to C:\
Then set webbrowser1 to navigate to C:\txt.html

Then try this
  showmessage(IntToStr(WebBrowser1.OleObject.Document.frames.Length));
  for f := 0 to WebBrowser1.OleObject.Document.frames.Length - 1 do
    showmessage(intToStr(WebBrowser1.OleObject.Document.frames.item(f).document.Forms.Length));

It came up with 4 as expected, but then got access denied..

You can't just navigate to c:\txt.html. You must place it in inetpub\wwwroot and use http://locahost/txt.html.
I tried it that way and got the same result.
Eh?
I placed it on c:\ and naviagated to it just fine
Got the same result as if it was on the internet anyway
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Aqueath - Thanks for the msg and summary, but it doesn't really get me any further ahead then my post "Figured it out! Finally....." I do appreaciate the time you've put into it though... Just gota figure out the cross domain thing...
I was working on a MHTML (web archive)  processor and was having major problems with Frames
and IFrames. Then I ran accross a post on comp.infosystems.authoring.html about the cross-domain
security issue.

Now, I am not using IHTMLDocument2 anymore, I just use idHTTP and TRegEx to parse the HTML. When
I run across a frame/iframe, I download the source separately.

I am trying to recreate the technique used in this VB.Net article in Delphi:
http://www.codeproject.com/vb/net/MhtBuilder.asp#xx979211xx
mmmm
thats what I may have to do
A friend pointed out that Robo form handles frames and iframes so there must be some way - maybe it has it's own build in html engine. Who knows. Tis a shame...
So, is that what you are doing, building a tool to download entire websites either into a MHT file or a directory?
Negative,

Trying to get the form elements from frames.
I may have to parse the html and if there are frames load them up somehow and go from there...

Herm...
but WHY?
So I can fill them out
ANy particular site you are working with or just in general?
Just in general, I've got everything working but the cross-domain thing....
:)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks everyone for there feedback...
Crap was ment to hit A!