droefs / HLISA

A reimplementation of the Selenium API, emulating human interactions
https://pypi.org/project/HLISA/
GNU General Public License v3.0
72 stars 8 forks source link

Mouse cursor lose focus when loading or refreshing a new page #21

Open JingerTea opened 2 years ago

JingerTea commented 2 years ago
from selenium_toolbox.prep_webdriver import get_driver
from HLISA.hlisa_action_chains import HLISA_ActionChains

driver = get_driver()
driver.get("https://apple.com")

action = HLISA_ActionChains(driver)
action.move_to(100,100).perform()
driver.refresh()
action.move_to(200,200).perform()

In the example code above, the mouse will move from (0,0) to (100,100). When I refresh the page or load a new page, the mouse will take its current position as (0,0). In this case, (100,100) becomes (0,0), which cause the mouse to lose focus. When I perform the second action, it is moving the mouse from (100,100) to (300,300) instead of (0,0) to (200,200).

Is there any line of code that can reset the mouse back to top left corner, and calibrate it back to (0,0)

JingerTea commented 2 years ago
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();

To see the mouse move in action, you can enter this in chrome's console

JingerTea commented 2 years ago

A temporarily fix to this is to use the original action chain library to reset mouse location

ActionChains(driver).move_to_location(0,0).perform()
bkrumnow commented 2 years ago

To the best of my knowledge, we cannot get the mouse position without triggering mouse events (which is a hacky solution and not human-alike).

I think that we want to dive more into Selenium here. If we can grab the current position from Selenium itself, then we have a way to come around this. Alternatively, we should challenge the assumption above.

Side remark: Can we track if the mouse position got a reset?

droefs commented 2 years ago

Thank you for reporting this issue and providing a temporary fix JingerTea. We tried to come up with a solution that is human like and a solid permanent solution, but it is hard to come up with a robust solution that works in all cases. We will investigate using the ideas from above and publish and updated version of HLISA when we found a solution.

droefs commented 2 years ago

Does this issue still exist in HLISA version 1.5? I cannot reproduce the issue, also not in older versions of HLISA.