Button press simulation in TWebBrowser

Hey,

What is the easiest way to simulate a button press or link press on a web page loaded
to TWebBrowser? Can someone provide a code fragment please?

Thanks, iec
iecAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HypoviaxCommented:
I am not clear as to what you are refering to but if you want to clicfk a buttone or make the mouse click you can use:

mouse_event(p.x,p.y,0,0,0,0); Where p is the point containing the x and y coordinates

keybd_event(VIRTUAL KEY CODE HERE (e.g. vk_return),0,0,0,0) ;

Check help to make sure i spealt the functions correctly

Regards,

Hypoviax
iecAuthor Commented:
I mean simulation, but without real mouse clicks or keyboard events, because the
application should be able to run as minimized, not influencing other running applications.
I need things like button clicks, link clicks, perhaps form submit, may be even some
entering of data via fill-in (input text element). But I would prefer using some real access
to the page, like FORM.submit and similar (for example to gain access to links field, choose one element of it and simulate a click on it, something like direct access to javascript objects if it is possible).
I saw in some question here nice approach using TWebBrowser.Document
and then .Body.OuterHTML for getting page source, when I went to help it directed me to MS DHTML reference, but not yet sure how to use it, so thinking someone could help me to advance faster. I imagine it something like: Link := Body.Links[3];  Link.Click();  LOL

Thanks again,
iec
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Eddie ShipmanAll-around developerCommented:
This is assuming there is only one form and only one submit button on the form:

WebBrowser1.OleObject.document.forms.item(0).submit();
iecAuthor Commented:
Thanks, I will browse through it in detail and try to find there some milestones. I was using autoit some years ago, so I am familiar with that one, but its disadvantage is, keyboard and mouse cant be used by other applications, what I cant afford now. I would like something similar, like stuayre wrote in Q_20792572 that its possible in visual basic. To press a button, where I know its caption. Not to press a button by its position simulating a mouseclick, that approach I dont like here.

iecAuthor Commented:
BTW I was trying to look at document object model (DOM). Its understandable down to interface Node, then I am stuck. Thought links.item(x).nodename and links.item(x).nodevalue would help further but it doesnt. Nodevalue should be a string type, but seemingly it isnt.
Eddie ShipmanAll-around developerCommented:
My last answer worked like a charm. I even tested it on one of my php forms on a site
that I manage.

Please try it before getting deeper into the DOM.

If you have multiple forms, all you have to do is
loop through them and check to see if it is the one
you want like this:

for i := 0 to WebBrowser1.OleObject.document.forms.Length-1 do
begin
  if Uppercase(IHTMLFormElement(WebBrowser1.OleObject.document.forms.items(i)).name) = Uppercase('Storegame') then
    WebBrowser1.OleObject.document.forms.item(i).submit();
  // You can also check the Action
  // Uppercase(if IHTMLFormElement(WebBrowser1.OleObject.document.forms.items(i)).action) = Uppercase('/storegame.php') then
  //  WebBrowser1.OleObject.document.forms.item(i).submit();
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerCommented:
Remember, that all buttons belong to a specific form and you must know the form
that it belongs to. Otherwise, if you want, I can show you how to get the button with a
specific name and actually click it using the DOM.

Let me know if you want to do that instead.
iecAuthor Commented:
OK, I had success with buttons now,
if (Element.tagName = 'INPUT') and (Element.Type ='submit') and (Element.Value ='button-name-here') then Element.click();
and the second approach you suggest by submitting the form both work.
Now I have to look after similar solution with other types of clickable elements, like
links etc.
Eddie ShipmanAll-around developerCommented:
The IHTMLElement object pertains to all elements and that includes links
(anchors) and you can call their click method just like a button.
iecAuthor Commented:
I see, HTMLLinkElement and it can be identified by href attribute the same way like button - HTMLButtonElement was identified by type and value attributes.
Everything is in section 2.5.5. Object definitions of DOM, just needed to learn to read LOL.

Thanks,
iec
Eddie ShipmanAll-around developerCommented:
Well, you'd have to cast the Element to a IHTMLLinkElement to do that but
I think IHTMLElement already has an href property.

  IHTMLLinkElement(Element).href
iecAuthor Commented:
One more question: Is there some easy way to remove the click sound?
Eddie ShipmanAll-around developerCommented:
It comes from your windows sounds settings.You can change it here in the registry
before navigating and then change it back after navigating:

{May be different on Win98 or Win2K, this is from XP Pro}
HKEY_CURRENT_USER\AppEvents\Schemes\Apps\Explorer\Navigating\.Current


iecAuthor Commented:
Nice, it works too, thanks.
iecAuthor Commented:
Little additional question: when I open link from the TWebBrowser component in new window, session data - login info etc is lost, is there some quick way to preserve session/cookie data as if it would be new window in internet explorer? And is there some quick way how to make clipboard copy within TWebBrowser (ctrl+C) work?
iecAuthor Commented:
thought there is some way to raise points but I dont see it here
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.