Solved

Button press simulation in TWebBrowser

Posted on 2003-11-14
17
1,201 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:iec
  • 9
  • 7
17 Comments
 
LVL 5

Expert Comment

by:Hypoviax
ID: 9752667
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
0
 

Author Comment

by:iec
ID: 9753500
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
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9767067
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9767206
This is assuming there is only one form and only one submit button on the form:

WebBrowser1.OleObject.document.forms.item(0).submit();
0
 

Author Comment

by:iec
ID: 9774783
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.

0
 

Author Comment

by:iec
ID: 9774916
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.
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 40 total points
ID: 9776052
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;
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9776060
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:iec
ID: 9776135
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.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9776297
The IHTMLElement object pertains to all elements and that includes links
(anchors) and you can call their click method just like a button.
0
 

Author Comment

by:iec
ID: 9776355
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
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9779246
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
0
 

Author Comment

by:iec
ID: 9781236
One more question: Is there some easy way to remove the click sound?
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9781491
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


0
 

Author Comment

by:iec
ID: 9781946
Nice, it works too, thanks.
0
 

Author Comment

by:iec
ID: 9939054
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?
0
 

Author Comment

by:iec
ID: 9939058
thought there is some way to raise points but I dont see it here
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now