Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2012, 03:16 PM
XOR XOR is offline
Junior Member
 
Join Date: Jun 2012
Posts: 12
Default WebBrowser1 & Image Submit Button

Hello guys,

I have troubles with clicking on images within the webbrowser in Delphi. All I need it to do is click a image once because it'll move the position of the player to the left or right. I've got a code but it gives me a access violation everytime I run it.

Here is the code I am using.

Code:
var
  i: Word;
  Document: IHtmlDocument2;
  str: string;
begin
  // Schleife über alle Bilder im Webbrowser
  for i := 0 to WebBrowser1.OleObject.Document.Images.Length - 1 do
  begin
    Document := WebBrowser1.Document as IHtmlDocument2;
    // URL auslesen
    Str := (Document.Images.Item(i, 0) as IHTMLImgElement).Href;
    // Dateiname des Bildes überprüfen
    if Pos('submit_icon.gif', str) = 0 then
    begin
      ((Document.Images.Item(i, 0) as IHTMLImgElement) as IHTMLElement).Click;
    end;
  end;
Does anybody else has a better solution for this. I really need this asap.

This is the source of the image:

PHP Code:
<img style="cursor:pointer;" width="25" height="24" onclick="PlayRequest(7,12, 4);" src="http://static.pokemonvortex.org/images/maps/arrows/arrowright.gif"
Reply With Quote
  #2  
Old 06-21-2012, 09:22 PM
Ouiji Ouiji is offline
Senior Member
 
Join Date: Nov 2001
Location: US of A
Posts: 478
Send a message via AIM to Ouiji
Default

Should the Document.Items.Images.length actually be Document.Items.images.count? .Length seems strange, and it would certainly throw off the loop and give an access violation if its the wrong property.

- ouiji
__________________
"not quite smart enough to be dumb"
Extended Stats No Longer Available Due To Changes To The Forum.
Reply With Quote
  #3  
Old 06-22-2012, 10:23 AM
XOR XOR is offline
Junior Member
 
Join Date: Jun 2012
Posts: 12
Default

I'll give that a try. It certainly gives me a access violation error with the previous one. I'll let you know the results once I tested it.

Thank you for your answer!

EDIT:

Unfortunately, that doesn't work either. It gives me a: ' Method Count Not Supported By Automation Object '.

Any suggestions?

Last edited by XOR; 06-22-2012 at 10:31 AM.
Reply With Quote
  #4  
Old 06-22-2012, 11:19 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 6,699
Default

You loop throug WebBrowser1.OleObject.Document but seem to want to use WebBrowser1.Document
But you get an AV, which line gives you the AV? Have you tried the debugger?
Reply With Quote
  #5  
Old 06-22-2012, 12:19 PM
XOR XOR is offline
Junior Member
 
Join Date: Jun 2012
Posts: 12
Default

I'm getting the error on this line:

Code:
for i := 0 to WebBrowser1.OleObject.Document.Images.Count - 1 do
also if you change it to this:

Code:
for i := 0 to WebBrowser1.OleObject.Document.Images.Count do
it'll give the same error.

---

it has to click the image only once.

- XOR
Reply With Quote
  #6  
Old 06-22-2012, 02:38 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 6,699
Default

You haven't read my previous reply properly, I started with telling you that you use 2 different objects. It should be 1 in all cases, either the WebBrowser1.OleObject.Document or the WebBrowser1.Document...

Given the error you get on the for loop (where you use the OleObject) try using WebBrowser1.Document instead...
Reply With Quote
  #7  
Old 06-22-2012, 03:04 PM
XOR XOR is offline
Junior Member
 
Join Date: Jun 2012
Posts: 12
Default

Hello Norrit,

Sorry I did not fully understand what you said in the previous post. I've done as you suggested but stumbled upon this:

Code:
[Error] Unit1.pas(1421): Undeclared identifier: 'Images'
with this:

Code:
for i := 0 to WebBrowser1.Document.Images.Count do
as I am pretty new with the webbrowser I do not fully understand it yet.

- XOR

Last edited by XOR; 06-22-2012 at 03:27 PM.
Reply With Quote
  #8  
Old 06-25-2012, 04:38 PM
Ouiji Ouiji is offline
Senior Member
 
Join Date: Nov 2001
Location: US of A
Posts: 478
Send a message via AIM to Ouiji
Default

I'm not 100% sure why... But I cannot seem to wrap my head around how that code is intended to work. But in looking for an answer, I stumbled upon this piece of code which should do roughly the same thing. Hopefully it is helpful.

Code:
uses
  MSHTML;

var
  iDoc: IHtmlDocument2;
  i: integer;
  ov: OleVariant;
  iDisp: IDispatch;
  iColl: IHTMLElementCollection;
  InputImage: HTMLInputImage;
begin
  WebBrowser1.ControlInterface.Document.QueryInterface(IHtmlDocument2, iDoc);
  if not Assigned(iDoc) then
  begin
    Exit;
  end;
  ov := 'INPUT';
  iDisp := iDoc.all.tags(ov);
  if Assigned(IDisp) then
  begin
    IDisp.QueryInterface(IHTMLElementCollection, iColl);
    if Assigned(iColl) then
    begin
      for i := 1 to iColl.Get_length do
      begin
        iDisp := iColl.item(pred(i), 0);
        iDisp.QueryInterface(HTMLInputImage, InputImage);
        if Assigned(InputImage) then
        begin
          if InputImage.Name = 'submit' then
          // if the name is submit / falls der name submit lautet
          begin
            InputImage.Click;  // click it / klick es
          end;
        end;
      end;
    end;
  end;
end;



- ouiji
__________________
"not quite smart enough to be dumb"
Extended Stats No Longer Available Due To Changes To The Forum.
Reply With Quote
  #9  
Old 06-26-2012, 12:18 PM
XOR XOR is offline
Junior Member
 
Join Date: Jun 2012
Posts: 12
Default

Hello Ouiji,

Thank you for your answer, but that one doesn't work while the previous one worked like a charm but gave me a Access Violation after each time I pressed the button.

The problem is, the image does not have a specific name. While the tags are correct. I've checked the attributes through google chrome and it appears that they are all the same except for the src="".

The previous code I used searches for the image name for example:

PHP Code:
"arrowright.gif" 
and if he found the image, he clicks on it. But somehow it gives me a access violation after it. If I use the source you provided and I do not enter a image name, then it does click on one of those images, but it also clicks on all other images with the same TAG.

Maybe there is a way to click on image without moving the cursor at all, but I've been digging for days now and hoping to find a solution but yet with no luck.

---

Kind Regards,
XOR
Reply With Quote
  #10  
Old 06-26-2012, 10:30 PM
Ouiji Ouiji is offline
Senior Member
 
Join Date: Nov 2001
Location: US of A
Posts: 478
Send a message via AIM to Ouiji
Default

It does not work when you change the code to if image.name = 'arrowright.gif' then..? I tried it on a simple html example, and it was working for me, does it not trigger?
__________________
"not quite smart enough to be dumb"
Extended Stats No Longer Available Due To Changes To The Forum.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 01:51 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.