java指针偏移量,java-Selenium视图鼠标/指针

最后,我不得不使用Java机器人来完成这项工作.不仅是为了看到鼠标,还因为对于HTML5 Web App来说,硒中的拖放操作是中断的,因为拖放需要两个动作才能进行注册.硒只做一个.

我的方法从每个对象的中心拖动,如果要拖动到要拖动到的元素上方,则可以使用偏移量.

public void dragAndDropElement(WebElement dragFrom, WebElement dragTo, int xOffset) throws Exception {

//Setup robot

Robot robot = new Robot();

robot.setAutoDelay(50);

//Fullscreen page so selenium coordinates are same as robot coordinates

robot.keyPress(KeyEvent.VK_F11);

Thread.sleep(2000);

//Get size of elements

Dimension fromSize = dragFrom.getSize();

Dimension toSize = dragTo.getSize();

//Get centre distance

int xCentreFrom = fromSize.width / 2;

int yCentreFrom = fromSize.height / 2;

int xCentreTo = toSize.width / 2;

int yCentreTo = toSize.height / 2;

//Get x and y of WebElement to drag to

Point toLocation = dragTo.getLocation();

Point fromLocation = dragFrom.getLocation();

//Make Mouse coordinate centre of element and account for offset

toLocation.x += xOffset + xCentreTo;

toLocation.y += yCentreTo;

fromLocation.x += xCentreFrom;

fromLocation.y += yCentreFrom;

//Move mouse to drag from location

robot.mouseMove(fromLocation.x, fromLocation.y);

//Click and drag

robot.mousePress(InputEvent.BUTTON1_MASK);

//Drag events require more than one movement to register

//Just appearing at destination doesn't work so move halfway first

robot.mouseMove(((toLocation.x - fromLocation.x) / 2) + fromLocation.x, ((toLocation.y - fromLocation.y) / 2) + fromLocation.y);

//Move to final position

robot.mouseMove(toLocation.x, toLocation.y);

//Drop

robot.mouseRelease(InputEvent.BUTTON1_MASK);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值