You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行Sortable功能测试代码后,待办列表未成功移至完成区

拖拽元素失败的排查与解决方法

核心问题定位

你的测试代码里存在致命错误:From和To使用了同一个元素ID(sample-box1),相当于把元素拖拽到自身位置,自然无法移动到Done区域。

修正方案

1. 修正目标元素定位

Done区域的容器ID为sample-output,需要将目标定位到该容器,而非待拖拽元素本身。示例代码如下:

driver.get("https://letcode.in/sortable");

// 定位待办区域的元素
WebElement fromElement = driver.findElement(By.id("sample-box1"));
// 定位完成区域的容器
WebElement targetArea = driver.findElement(By.id("sample-output"));

Actions actions = new Actions(driver);
// 使用dragAndDrop方法直接完成拖拽
actions.dragAndDrop(fromElement, targetArea).build().perform();

2. 补充额外排查与优化点

  • 确保元素可交互:拖拽前等待元素完全加载并处于可交互状态,避免因元素未就绪导致动作失效:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement fromElement = wait.until(ExpectedConditions.elementToBeClickable(By.id("sample-box1")));
WebElement targetArea = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("sample-output")));
  • 模拟真实拖拽动作:如果dragAndDrop方法无效,可模拟人工拖拽的完整流程(包含停顿、精准定位目标中心):
actions.clickAndHold(fromElement)
        // 移动到目标区域的中心位置
        .moveToElement(targetArea, targetArea.getSize().getWidth()/2, targetArea.getSize().getHeight()/2)
        .pause(Duration.ofMillis(500)) // 适配页面动画的停顿时间
        .release()
        .build()
        .perform();
  • 检查驱动兼容性:确认浏览器驱动(如ChromeDriver)版本与浏览器版本完全匹配,版本不兼容会导致Actions类的拖拽动作失效。
  • 排查页面嵌套结构:若页面存在iframe,需先切换到对应iframe再进行元素定位(当前测试页面无iframe,但可作为通用排查点)。

内容的提问来源于stack exchange,提问作者Bhuvana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 10:27:37