Using Actions able to select element but not able drag element to particular location, because drop functionality is created on hover

前提是你 提交于 2020-05-15 03:03:29

问题


URL - http://www.seleniumeasy.com/test/drag-and-drop-demo.html

 System.setProperty("webdriver.chrome.driver", "D:\\Eclipse\\Files\\chromedriver_win32\\chromedriver.exe");
         driver = new ChromeDriver();   
      driver.manage().window().fullscreen();
         driver.get("http://www.seleniumeasy.com/test/drag-and-drop-demo.html");        
             Thread.sleep(5000);

         WebElement itemToBeDragged = driver.findElement(By.xpath("//div[@id='todrag']//span[3]"));
         WebElement whereToBeDragged = driver.findElement(By.xpath("//div[@id='mydropzone']"));

         Thread.sleep(3000);
         Actions builder = new Actions(driver);
         builder.clickAndHold(itemToBeDragged).moveToElement(whereToBeDragged).build();
         Thread.sleep(3000);
         builder.dragAndDrop(itemToBeDragged, whereToBeDragged).perform();

I already tried my solutions but none work for me. for ex.:-

  1. https://gist.github.com/rcorreia/2362544
  2. Java - drag and drop not working on selenium 3.8
  3. Not able to drag and drop element to another element using Selenium-Webdriver
  4. Not able to drag element using Actions

回答1:


Tried with most of the suggestions on SO and finally came up with this, I am very surprised cause drag and drop is achievable through a lot of ways but none of them seemed to work on this particular link, The below code seems to work fine ( Tried with all 4 draggables )

Used Robot class here

    driver.get("https://www.seleniumeasy.com/test/drag-and-drop-demo.html");

    driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);

    Point coordinates = driver.findElement(By.xpath("//div[@id='todrag']//span[3]")).getLocation();
    Point coordinatesa = driver.findElement(By.xpath("//*[@id='mydropzone']")).getLocation();
    Robot robot = new Robot();
    robot.mouseMove(coordinates.getX(), coordinates.getY() + 120);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseMove(coordinatesa.getX() + 100, coordinatesa.getY() + 130);
    Thread.sleep(500);
    robot.mouseMove(coordinatesa.getX() + 80, coordinatesa.getY() + 130);
    robot.delay(2000);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);


来源:https://stackoverflow.com/questions/61316093/using-actions-able-to-select-element-but-not-able-drag-element-to-particular-loc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!