【问题标题】:How to Drag if in app have Both Drag-able Elements?如果应用程序中有两个可拖动元素,如何拖动?
【发布时间】:2016-05-16 21:23:21
【问题描述】:

我是 selenium (Java) 的新手。在练习时,我发现这个页面的两个元素都可以拖动。我几乎一路尝试... http://the-internet.herokuapp.com/drag_and_drop

// ****Case 1****

    Actions builder = new Actions(driver);
    builder.dragAndDrop(fromWebElement, toWebElement);

// ****Case 2****

    Actions builder = new Actions(driver);
    Action dragAndDrop =
      builder.clickAndHold(fromWebElement).moveToElement(toWebElement)
        .release(toWebElement).build();
    dragAndDrop.perform();

// ****Case 3****
 Actions builder = new Actions(driver);
    Action dragAndDrop =
      builder.clickAndHold(fromWebElement).moveToElement(toWebElement, 2, 2)
        .release(toWebElement).build();
    dragAndDrop.perform();


 //****Case 4****
    Actions builder = new Actions(driver);
    builder.clickAndHold(fromWebElement).moveToElement(toWebElement).perform();
    Thread.sleep(2000);// add 2 sec wait
    builder.release(toWebElement).build().perform();


 //****Case 5****
    Point coordinates1 = fromWebElement.getLocation();
    Point coordinates2 = toWebElement.getLocation();
        Robot robot = new Robot();
        robot.mouseMove(coordinates1.getX(), coordinates1.getY());
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(coordinates2.getX(), coordinates2.getY());
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        Thread.sleep(2000);

【问题讨论】:

  • 还是搞不定,,,,,

标签: java eclipse selenium-webdriver draggable


【解决方案1】:

试试下面的代码:

@Test
public void test() throws InterruptedException, AWTException{

    WebDriver driver = new FirefoxDriver();    

    driver.manage().window().maximize();
    driver.get("http://the-internet.herokuapp.com/drag_and_drop");
    Thread.sleep(3000);

    WebElement fromWebElement = driver.findElement(By.cssSelector("#column-a"));
    WebElement toWebElement = driver.findElement(By.cssSelector("#column-b"));

    int x = toWebElement.getLocation().getX();
    int y = toWebElement.getLocation().getY();

    Point coordinates = driver.findElement(By.cssSelector("#column-a")).getLocation();

     Robot robot = new Robot();
     robot.mouseMove(coordinates.getX()+120,coordinates.getY()+120);
     Thread.sleep(1000);

    Actions builder1 = new Actions(driver);
    builder1.clickAndHold(fromWebElement).moveToElement(toWebElement).release().build().perform();
    Thread.sleep(1000);
    Point coordinates1 = driver.findElement(By.cssSelector("#column-b")).getLocation();
    Robot robot1 = new Robot();
    robot1.mouseMove(coordinates1.getX()+120,coordinates1.getY()+120);


    Thread.sleep(1000);
    Robot bot = new Robot();
    bot.mouseMove(x +120, y+120);    
    bot.mousePress(InputEvent.BUTTON1_MASK);
    bot.mouseRelease(InputEvent.BUTTON1_MASK);
}

希望这会对你有所帮助。

【讨论】:

  • 没有错误,对象甚至没有移动到第二个对象位置(意味着A没有拖到B侧)
  • 我在更新的答案中给出了我的完整测试用例...运行这个.....如果没有成功,很抱歉,因为它在我的机器上运行良好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 2016-07-16
  • 1970-01-01
相关资源
最近更新 更多