我尝试使用Robot框架自动执行拖放功能。为了实现这一点,我写了以下代码:
WebDriver driver = new FirefoxDriver();
driver.get("http://the-internet.herokuapp.com/drag_and_drop");
WebElement source = driver.findElement(By.id("column-a"));
WebElement target = driver.findElement(By.id("column-b"));
Robot robot = new Robot();
robot.mouseMove(source.getLocation().getX() + (source.getSize().getWidth()/2), source.getLocation().getY() + (source.getSize().getHeight()/2));
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(target.getLocation().getX() + (target.getSize().getWidth()/2), target.getLocation().getY() + (target.getSize().getHeight()/2));
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(3000);
执行上面的代码,我可以看到鼠标正在移动到源和目标,但拖放并没有发生。能告诉我这里我做错了什么吗?
答案 0 :(得分:0)
您可以尝试使用此方法 - 获取源代码,请将其与您的代码嵌入:
WebElement source = driver.findElement(By.xpath("//*[@id='column-a']"));
Locatable element = (Locatable)source;
Point p= element.getCoordinates().inViewPort();
int sourceX=p.getX()+25;(25 can be increased or decreased accordingly)
int sourceY=p.getY()+120;(120 can be increased or decreased accordingly)
对于目的地,请将其与您的代码嵌入:
WebElement target = driver.findElement(By.id("column-b"));
Locatable elementTarget = (Locatable)target;
Point Target= elementTarget.getCoordinates().inViewPort();
int targetX=Target.getX()+55;
int targetY=Target.getY()+150;
答案 1 :(得分:0)
我对JTable和JList都有类似的问题。
对于桌子,我不得不用桌子拖得足够远,或者它永远不会变成拖动手势。
有了一个列表,我不仅需要走得足够远,而且还必须在中间添加一个方法点并在那里停留足够长的时间以便它能够实现正在发生的事情。否则,它尝试使用零长度的DataFlavor数组将其放在那里。
结束机器人代码,如:
public void dragAndDrop(Point start, Point end) {
// Press
mouseMove(start.x, start.y);
delay(100);
mousePress(MouseEvent.BUTTON1_MASK);
delay(500);
// Drag - through a way point, so this will work on JLists - make sure you linger long enough at the way point too!
mouseMove(start.x - (start.x - end.x)/2, start.y - (start.y - end.y)/2);
delay(500);
mouseMove(end.x, end.y);
delay(1000); // So you can see what is happening
mouseRelease(MouseEvent.BUTTON1_MASK);
// Wait (copied from other code)
waitForIdle();
delay(200);
}