Search code examples
javascriptc#seleniumclearscript

In Selenium, moving to an (x,y) on browser screen doesn't move mouse pointer. Why?


Context: Windows 10; C# .NET; Selenium WebDriver (Chrome); ClearScript

I've exposed a number of Selenium functions to a javascript via ClearScript. In the script ...

var url = "http://www.google.com.au";
var driver = new CSChromeDriver();
driver.Navigate().GoToUrl(url);

...

var topLeft = driver.FindElement(CSBy.TagName("body"));
var builder = new CSActions(driver);
var tl = builder.MoveToElement(topLeft, 200, 200);
tl.Build().Perform();

... I create an instance of a Chrome webdriver, navigate to a web site, find the "body" tag, create an instance of the Actions object, and using that, move to an (x,y) of (200,200) from the body's top-left corner.

I've got code in there (not shown) which tells me where the browser thinks the mouse is. After the above, the browser reports to me that it's internal (x,y) is (200,200) which is great. What is less great is that the on-screen mouse-pointer hasn't moved to match.

Is there any way to tell the mouse-pointer to reflect the browser's inner reality?


Solution

  • moveToElement will not change the mouse pointer to point to that co-ordinate. I will make the your variable 'tl' refer to the element present in that co-ordinate and you can perform operations like click on it.

    No, selenium does not have any in built method to make the mouse pointer go from one location to another ( by showing the transition i mean ).

    If you want to highlight the element on which the current action is being performed, you can do that in selenium using javascript executer

    JavascriptExecutor js=(JavascriptExecutor)driver;
    js.executeScript("arguments[0].setAttribute('style,'border: solid 2px red'')", username);