java - 使用 Action 可以选择元素但不能将元素拖到特定位置,因为放置功能是在悬停时创建的

标签 java selenium selenium-webdriver

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

我已经尝试过我的解决方案,但没有一个对我有用。
例如:-
  • https://gist.github.com/rcorreia/2362544
  • Java - drag and drop not working on selenium 3.8
  • Not able to drag and drop element to another element using Selenium-Webdriver
  • Not able to drag element using Actions
  • 最佳答案

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

    在这里使用机器人类(class)

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

    关于java - 使用 Action 可以选择元素但不能将元素拖到特定位置,因为放置功能是在悬停时创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61316093/

    相关文章:

    java - JMapViewer,获取OSM map 比例尺,Java

    java - 如何在java中删除字符串 "\u001b[1m"

    java - 在变量之间传递大数据时最大限度地减少内存使用

    python - 需要从 selenium 服务器转储带有元素 id 的整个 DOM 树

    javascript - Driver.quit() 不会关闭浏览器

    java - org.apache.cxf.interceptor.Fault : Marshalling Error: null

    perl - 如何在 perl 脚本中使用 Selenium Remote Driver 最大化/最小化 firefox 浏览器窗口

    java - 验证网页上的文本,然后使用 Java 获取 selenium webdriver 中的位置

    javascript - 访问从 javascript 函数 webdriver 动态生成的 href

    Selenium xpath - 使用 <bean :define> tag name 定位元素