6

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?

2
  • 1
    Not sure but this may help! Commented Dec 23, 2018 at 4:22
  • @CoderCroc the answer you are referring to creates a mouse and focuses on it (if I understand correctly). Selenium WebDriver has the Actions module that does this perfectly! see my answer... Commented Dec 23, 2018 at 15:02

3 Answers 3

2

You can highlight the elements that you are interacting with usings JS.

    String jsSyyle = "'3px solid red'";
    WebElement element; 
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].style.border=" + jsSyyle, element);
Sign up to request clarification or add additional context in comments.

Comments

2

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

1

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

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.
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.
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.