2015-02-07 49 views
0

我目前发现一些代码,应该允许我自动拖放代码,它有参数中的web元素。应该在这些参数中放置什么?我已经使用以下内容:在方法中使用webelements

driver.findElements(By.linkText("Policy")); 
findElements(By.linkText("Policy"));           
By.linkText("Policy"); 

这些都没有为我工作波纹管是我使用的代码的副本。

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 work 
    robot.keyPress((InputEvent.BUTTON1_MASK)); 
    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 
    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

要使用我创建的参数所需要的对象的方法,希望这有助于

WebElement dragFrom = driver.findELement(By.id("id")); 
WebElement dragTo = driver.findELement(By.id("id")); 

dragAndDropElement(dragFrom,dragTo, 10);