Solved

Button press simulation in TWebBrowser

Posted on 2003-11-14
17
1,231 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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