![]() |
|
#1
|
|||
|
|||
|
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;
![]() This is the source of the image: PHP Code:
|
|
#2
|
|||
|
|||
|
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.
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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? |
|
#5
|
|||
|
|||
|
I'm getting the error on this line:
Code:
for i := 0 to WebBrowser1.OleObject.Document.Images.Count - 1 do Code:
for i := 0 to WebBrowser1.OleObject.Document.Images.Count do --- it has to click the image only once. ![]() - XOR |
|
#6
|
|||
|
|||
|
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... |
|
#7
|
|||
|
|||
|
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' Code:
for i := 0 to WebBrowser1.Document.Images.Count do ![]() - XOR Last edited by XOR; 06-22-2012 at 03:27 PM. |
|
#8
|
|||
|
|||
|
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.
|
|
#9
|
|||
|
|||
|
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:
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 |
|
#10
|
|||
|
|||
|
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.
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|