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?
 
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;
0
 
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
0
 
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
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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();
0
 
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.

0
 
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.
0
 
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.
0
 
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.
0
 
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.
0
 
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
0
 
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
0
 
iecAuthor Commented:
One more question: Is there some easy way to remove the click sound?
0
 
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


0
 
iecAuthor Commented:
Nice, it works too, thanks.
0
 
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?
0
 
iecAuthor Commented:
thought there is some way to raise points but I dont see it here
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.