我正在尝试将元素拖放到另一个元素上,但我无法这样做。我正在尝试在http://the-internet.herokuapp.com/drag_and_drop
上执行上述操作这是我的代码
we1=driver.findElement(By.xpath("//*[@id='column-a']"));
we2=driver.findElement(By.xpath("//*[@id='column-b']"));
action.clickAndHold(we1);
action.moveToElement(we2, 450, 250);
Thread.sleep(3000);
action.release().build().perform();
我尝试使用action.moveToElement(we2,450,250)
和action.moveToElement(we2)
而不是action.moveByOffset(450,250)
。
我甚至尝试使用Robot类执行此操作。
robot.mouseMove(230, 170);
Thread.sleep(3000);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseMove(450, 200);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
上述任何代码均未发生任何变化。不会抛出任何错误或异常,并且代码执行成功完成。
答案 0 :(得分:1)
经过大量努力(点击并尝试:))我能够找到解决问题的方法。它适用于我提到的网站以及存在拖放功能的其他网站。这是解决方案:
we1CenterX=we1Points.x+we1Dimensions.width/2;
we1CenterY=we1Points.y+we1Dimensions.height/2;
we2CenterX=we2Points.x+we2Dimensions.width/2;
we2CenterY=we2Points.y+we2Dimensions.height/2;
robot.mouseMove(we1CenterX, we1CenterY);
waitForMe(1); //custom wait
robot.mousePress(InputEvent.BUTTON1_MASK);
waitForMe(1);
robot.mouseMove(we2CenterX, we2CenterY);
waitForMe(1);
robot.mouseMove(we2CenterX+100, we2CenterY-70);
/*Moving the cursor over the element before releasing
the mouse button solved my problem*/
waitForMe(1);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
waitForMe(1);