JavaScript webdriver-selenium `mouseMove` inaccura

2019-09-18 10:35发布

问题:

Running Chromedriver through NodeJs - protractor and selenium-webdriver - against an embedded Chromium on Windows 10.

It seems that using getLocation returns accurate positions, but when I try to have WebDriver move the mouse to those positions, the mouse doesn't get there -- unless I multiply the positions by about 1.45.

Surely that can't be right?

This is the first time I've used Selenium in years, but I'm sure I haven't had to do this before.

Below is an abbreviation of the code, with some of the webdriver log: 1. move the mouse to an element, 1. depress the mouse button, 1. wait a little for the system under to test to react, 1. move the mouse a little for the system under test, 1. wait a little, 1. move to a target element 1. release the mouse button.

The code works, provided I change nothing, and only move vertically.

If I move vertically, I see no effect.

If I run code twice in one Chromium window, loading the test document at the start of each run, the second run hangs around document loading.

var destLocation,
    fromEl = $('elementA'),
    destEl = $('elementB');

destEl.getLocation()
    .then(() => {
      browser.driver.actions()
        .mouseMove( fromEl, {x: 10, y: 10} )
        // 09:56:37.222 INFO - Done: [mousemove: 5 true]
        .mouseDown( fromEl, {x: 10, y: 10} )
        // 09:56:37.230 INFO - Done: [mousedown: no args]
        .perform();
    })
    .then(() => {
      browser.sleep(400);
    })
    .then(() => {
      // Business logic requires this
      browser.driver.actions().mouseMove( fromEl, {x:15, y:15} )
        // 09:56:37.719 INFO - Done: [mousemove: 5 true]
        .mouseDown()
        // 09:56:37.726 INFO - Done: [mousedown: no args]
        .perform();
    })
    .then(() => {
      browser.sleep(400);
    })
    .then(() => {
      browser.driver.actions().mouseMove( $('body'), destLocation ).perform();
      // 09:56:38.209 INFO - Done: [mousemove: 9 true]
    })
    .then(() => {
      browser.sleep(200);
    })
    .then(() => {
      browser.driver.actions().mouseUp().perform();
      // 09:56:38.425 INFO - Done: [mouseup: nothing]
    });
  • Selenium v2.52.0, with Core v2.52.0. Built from revision 4c2593c
  • Driver info: driver.version: RemoteWebDriver
  • OS: Windows 10 10.0 amd64
  • Java: Oracle Corporation 25.91-b14
  • chromedriver_2.21 and chromedriver_2.15.322448 seem to act the same way
  • CEF 3.2454.1344.g2782fb8
  • Chromium 45.0.2454.101
  • WebKit 537.36
  • JavaScript 4.5.103.35

回答1:

You can use this code for mouse hover in selenium javascript:-

const actions = driver.actions({bridge: true});
var elem=await driver.findElement(By.id("myId"));
await actions.move({duration:5000,origin:elem,x:0,y:0}).perform();

This code has to be inside an async function as await is used, or else to use with promise, this code might help:-

const actions = driver.actions({bridge: true});
driver.findElement(By.id("myId")).then((elem)=>{
  actions.move({duration:5000,origin:elem,x:0,y:0}).perform();
});

hope it helps....



回答2:

This was reported to the Chromium team as a bug, who could not reproduce the issue with the latest versions of all libraries.