I'm using Selenium Java WebDriver to automate UI tests. It works fine but it doesn't show the mouse pointer as it performs actions like clicking on a button. How can I make the mouse pointer visible as it moves on the page and clicks on a button?
3 Answers
The browser doesn't keep track of the cursor because that's the job of the operating system.
Thanks to this answer :Show mouse cursor position in selenium you can draw a cursor.
# python
enable_cursor = """
function enableCursor() {
var seleniumFollowerImg = document.createElement("img");
seleniumFollowerImg.setAttribute('src', 'data:image/png;base64,'
+ 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAeCAQAAACGG/bgAAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAA'
+ 'HsYAAB7GAZEt8iwAAAAHdElNRQfgAwgMIwdxU/i7AAABZklEQVQ4y43TsU4UURSH8W+XmYwkS2I0'
+ '9CRKpKGhsvIJjG9giQmliHFZlkUIGnEF7KTiCagpsYHWhoTQaiUUxLixYZb5KAAZZhbunu7O/PKf'
+ 'e+fcA+/pqwb4DuximEqXhT4iI8dMpBWEsWsuGYdpZFttiLSSgTvhZ1W/SvfO1CvYdV1kPghV68a3'
+ '0zzUWZH5pBqEui7dnqlFmLoq0gxC1XfGZdoLal2kea8ahLoqKXNAJQBT2yJzwUTVt0bS6ANqy1ga'
+ 'VCEq/oVTtjji4hQVhhnlYBH4WIJV9vlkXLm+10R8oJb79Jl1j9UdazJRGpkrmNkSF9SOz2T71s7M'
+ 'SIfD2lmmfjGSRz3hK8l4w1P+bah/HJLN0sys2JSMZQB+jKo6KSc8vLlLn5ikzF4268Wg2+pPOWW6'
+ 'ONcpr3PrXy9VfS473M/D7H+TLmrqsXtOGctvxvMv2oVNP+Av0uHbzbxyJaywyUjx8TlnPY2YxqkD'
+ 'dAAAAABJRU5ErkJggg==');
seleniumFollowerImg.setAttribute('id', 'selenium_mouse_follower');
seleniumFollowerImg.setAttribute('style', 'position: absolute; z-index: 99999999999; pointer-events: none; left:0; top:0');
document.body.appendChild(seleniumFollowerImg);
document.onmousemove = function (e) {
document.getElementById("selenium_mouse_follower").style.left = e.pageX + 'px';
document.getElementById("selenium_mouse_follower").style.top = e.pageY + 'px';
};
};
enableCursor();
"""
driver.execute_script(enable_cursor)
Comments
The way to do that is with Actions see documentation.
For example:
Actions action = new Actions(webdriver);
WebElement myElement = webdriver.findElement(By.xpath("the/xpath/to/element"));
action.moveToElement(myElement).click().build().perform();
Hope this helps you!
3 Comments
Shant Dashjian
Thanks for the reply. It seems that using the Actions class should do it, but so far it isn't. I tried using both Firefox and Chrome webdrivers to no avail. Mouse doesn't move. I'll investigate more.
Shant Dashjian
Do you actually see the mouse pointer moving inside the browser window from one place to another using the Actions class, or do you just see the effect? What version of selenium-server jar are you using? I'm using
3.141.59, on Mac, and Chrome latest 71.0.3578.98.Moshe Slavin
I do this on windows... but I don't think that is the problem... try this: add an element without clicking on it just move to it... maybe it's just too fast...
Actionsmodule that does this perfectly! see my answer...