Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 97778

Can you track mouse position on a webpage in Selenium webbrouser? C#

$
0
0

I am trying to keep track of mouse coordinates in the Selenium browser.

Selenium has no mouse tracking functions. It only has MoveByOffset(int x, int y) and MoveToElement(IWebElement) function. I need to set custom positions and move to them using the MoveByOffset(int x, int y) function.

Selenium allows usage of JavaScript script, but it seems not to allow running the script in the background and retrieving values from the said background function:

We could run something like:

document.onmousemove = function(e)
{
    var x = e.pageX;
    var y = e.pageY;
};

and return values from it, but it doesn't seem to be possible (explained above).

We could also create a Chrome extension and put the script in it's background.js, but I couldn't find a way to retrieve a value from the extension and to use it in code. Is this possible?

I also tried to keep track of the mouse movement in the C# code, but it doesn't seem to be reliable:

We usually need to scroll the page. It can be done with: js.ExecuteScript("window.scrollBy(0," + scrollDownAmountInPixels + ")");, where js is the IJavaScriptExecutor. But scrolling also moves the mouse. We could update the mouse-y value to the scrolled value, but if we are at the top of the page and we scroll up OR if we are at the bottom of the page and we scroll down - the mouse will recieve wrong coordinates. We could test for things like page height, window's distance from the top of the page (scroll amount) and for window's distance from the bottom of the page (page height - (scroll amount + window height size)), BUT JavaScript likes to return 0 instead of some of these values at some specific page location. I tried scrolling by pixel to find a location in which JavaScript will not return 0 in the needed values, but it just becomes too unreliable.

I really hope to find some help here. Thanks in advance!


Viewing all articles
Browse latest Browse all 97778

Trending Articles