Search code examples
javascriptvb.netawesomium

JavaScript programatically hover mouse over element


I'm writing a vb.net program to automate and manage an online game. I'm using the Awesomium webcontrols to display and manipulate the pages of the game.

There is a point where I need to grab the data that's not shown in the source until the user hovers over a certain element, how can I use javascript (Not jquery please) to hover over it programatically until the data I need becomes available and then grabbed?

I apologise if this has been asked before (Which it has but from the perspective of someone who owns the web page) but I have been searching for hours for a solution and cant find anything.

What I've tried to use but failed is:

function findBpDate(){  
    document.getElementById('tileDetails').children[1].children[0].children[1].children[0].fireEvent('onmouseover');    
    return document.getElementsByClassName('text elementText')[0].textContent;
}

This returns "undefined" when it calls back to my application, I'm certain I'm pointing to the right DOM elements though.

This is what I want the javascript to "hover" on:

   <span class="a arrow disabled">Send troops</span>

Once this element has been "hovered" on, this elements text changes to the text I need to grab:

   <div class="text elementText">Beginners protection until 20/07/13 07:51 am.</div>

I've shown above what the element looks like when the mouse "hovers" on the element I need it to, however this changes a lot depending on which element the user hovers over while playing the game, from what i gather it's where the source keeps the text for each tooltip in the game.

So I need a function that will hover over a certain element and then while it's hovering, grab the text from the tooltip text/"text elementText" element.


Solution

  • Try WebView.InjectMouseMove(x, y).

    Something like

        public Point GetElementPosition(dynamic element)
        {
            dynamic rect = element.getBoundingClientRect();
            using (rect)
            {
                return new Point(rect.left, rect.top);
            }
        }
    
        dynamic element = webView.ExecuteJavascriptWithResult("document.getElementById('id')");
        Point pos = GetElementPosition(element);
        webView.InjectMouseMove(pos.X, pos.Y);