Adrian Dorache

Hello!

I'm trying to automate IE so I need to simulate clicks on HTML elements. An easy way to do this is to call IHTMLElement::click() method. This cause the onclick event to be fired and the "default action" on the element is performed. By "default action" I mean navigation for <a> and area, form submit for <input type=submit> etc.

click() method works good enough but on some occasions the onclick handler needs the mouse coordinates from window.event object.

Question 1: is there any way to properly set mouse coordinates for the event generated by "click()" method

My second approach was to use fireEvent("onclick") method. This method fires the onclick event and I can set the mouse coordinates for the newly created event before fireing it. However fireing onclick event will not cause the "default action" to happen.

Question 2: is there any way to somehow make the default action to happen

Thanks in advance,
Adrian.




Re: Internet Explorer Extension Development fireEvent("onclick") vs click method

John Sudds - MSFT

RE: Question #2

Many of the elements in an HTML page (such as links and buttons) expose an IAccessible interface through IServiceProvider::QueryService (IID_IAccessible). Once you have a pointer to the interface, call accDoDefaultAction (CHILDID_SELF). This will post a message into the queue so it should react as if it were actually clicked by the user.

Some elements may not support the IAccessible interface, and others will return E_INVALIDARG when you attempt to perform a default action. So, do this in your primary attempt, and simulate the click in the usual way if it doesn't work.

Let us know how it goes.






Re: Internet Explorer Extension Development fireEvent("onclick") vs click method

rocoder

First of all, thank you for your quick reply.
Unfortunately the IAccessible solution is not exactly what I'm looking for. I performed a quick test and it seems that calling accDoDefaultAction is basically the same as calling IHTMLElement::click.
Onclick event is raised and default action occurs. So I still need a way to set the coordinates for the onclick event.

Adrian.






Re: Internet Explorer Extension Development fireEvent("onclick") vs click method

John Sudds - MSFT

My wise contacts over in IE-land suggest that you try moving the mouse cursor before you invoke the click (however you do it). The following code will not only "set the mouse coordinates" for a click event, but also trigger an onmouseover event.

Code Snippet

LONG lLeft = 0;

LONG lTop = 0;

HRESULT hr = pWebBrowser2->get_Left(&lLeft);

if (SUCCEEDED(hr))

{

hr = pWebBrowser2->get_Top(&lTop);

}

//Move mouse outside of the IE client area

if (SUCCEEDED(hr))

{

if (!SetCursorPos(0, 0))

{

hr = HRESULT_FROM_WIN32(GetLastError());

}

}

//Move mouse inside the IE client to toggle mouseover event

if (SUCCEEDED(hr))

{

if (!SetCursorPos( (lLeft + 100), (lTop + 100) ))

{

hr = HRESULT_FROM_WIN32(GetLastError());

}

}

Once the cursor is in place, the click event should be able to accurately report the position as part of its normal processing. Check out the sample for getBoundingClientRect method to see how to locate elements. You will need to experiment with the code so that you can adequately place the cursor.

http://msdn2.microsoft.com/en-us/library/ms536433.aspx

PS: IAccessible is superior to IHTMLElement::click only in that IE considers the click as a true user-initiated action (with all its inherent goodness).






Re: Internet Explorer Extension Development fireEvent("onclick") vs click method

rocoder

Yes, I think that will do it. I forgot to mention that I try to automate IE even if the HTML window is not in foreground

so it seems that SetCursorPos is not an option for me.

One solution I'm thinking about is to PostMessages WM_LBUTTONDOWN, WM_LBUTTONUP to

"Internet Explorer_Window" window (with the right coordinates).

It seems that it works even if the window is not in foreground but I'm not very happy with it because it's not using browser features

and I'm not sure how reliable it actually is. Is more like a hack. What do you think

Adrian.






Re: Internet Explorer Extension Development fireEvent("onclick") vs click method

John Sudds - MSFT

Let me be the first to point out that what you doing is a workaround to a shortcoming in the IE event model. According to the W3C standard, fireEvent should trigger the default action, but it doesn't. For the short term anyway, a hack is the only option available.

I like the solution you came up with because it doesn't actually move the cursor, which is better from a usability standpoint.






Re: Internet Explorer Extension Development fireEvent("onclick") vs click method

rocoder

Thank you for all your effort!

Adrian.