WebView GetHitTestResult() shows previous node when simulating mouse clicks

I need to simulate clicking on nodes in HTML pages inside a WebView, then log which node was clicked. However, my simulated clicks are showing the previously-selected node instead of the currently-selected node when I call GetHitTestResult() from the Touch event handler in my subclassed WebView. Actual physical mouse clicks show the correct node. (On physical mouse clicks, the "Down" event shows the previous node but the "Up" event shows the current/correct node. On simulated clicks, the previous node shows for both the "Down" and "Up" events.) Unfortunately, I don't have any control over most of the web pages, so I can only do the node detection from my logging code.

Is there something I need to call between the simulated "Down" click and the simulated "Up" click for the node change to be recognized by Android / Xamarin / the WebView widget? Or is there any way in Android to force it to process any outstanding DispatchTouchEvent events that might still be pending in the UI thread? (I'm wondering if the problem is caused by something that just hasn't been processed between the fake "Down" click and the fake "Up" click.)

"Clicking" code:

void ClickThePage(WebView webView, float left, float top) {

    long downTime = Android.OS.SystemClock.UptimeMillis();
    long eventTime = Android.OS.SystemClock.UptimeMillis() + 100;
    MotionEvent motionEventDown = MotionEvent.Obtain(
        downTime, eventTime, MotionEventActions.Down,
        left, top, (MetaKeyStates)0x0
    );

    downTime = Android.OS.SystemClock.UptimeMillis() + 200;
    eventTime = Android.OS.SystemClock.UptimeMillis() + 300;
    MotionEvent motionEventUp = MotionEvent.Obtain(
        downTime, eventTime, MotionEventActions.Down,
        left, top, (MetaKeyStates)0x0
    );

    // actually do the "Click"
    webView.DispatchTouchEvent (motionEventDown);
    System.Threading.Thread.Sleep (50);
    webView.DispatchTouchEvent (motionEventUp);

    motionEventDown.Recycle ();
    motionEventUp.Recycle ();
}
Sign In or Register to comment.