使用操作可以选择元素,但无法将元素拖动到特定位置,因为放置功能是在悬停时创建的

2023-12-13

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();

我已经尝试过我的解决方案,但没有一个对我有用。 例如:-

  1. https://gist.github.com/rcorreia/2362544
  2. Java - 拖放不适用于 selenium 3.8
  3. 无法使用 Selenium-Webdriver 将元素拖放到另一个元素
  4. 无法使用操作拖动元素

尝试了大多数关于SO的建议,最后提出了这个,我很惊讶,因为拖放可以通过很多方式实现,但它们似乎都不适用于这个特定的链接,下面的代码似乎工作正常(尝试了所有 4 个可拖动对象)

这里使用了机器人类

    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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用操作可以选择元素,但无法将元素拖动到特定位置,因为放置功能是在悬停时创建的 的相关文章

随机推荐