Question

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.

Was it helpful?

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);

OTHER TIPS

this is 10x easier with js/dom. http://jsfiddle.net/pA2Vd/

Do this...assuming you can get reference to elements somehow using by Id would have been lot easier.

var elm = document.getElementsByClassName('a arrow disabled')[0];
var txt = document.getElementsByClassName('text elementText')[0];
var evt = new Event('mouseover');
elm.dispatchEvent(evt);
var status = txt.innerText;

(helpfuL stuff down) otherwise you need to capture event, detect who fired it, check if that has this class and tag name. Lot of processing.

var txt,spn,status='';
document.getElementByTagName('span').forEach(function(d){
    if (d.tagName=="div" && d.className == 'text elementText'){
    var txt = d;
    }    
}

window.onmouseover = function(e) {
    var elm = e.target;
    if (elm.tagName=="SPAN" && elm.className == 'a arrow disabled') {
       status=txt.innerText;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top