Selenium 3.8中Drag and Drop功能失效问题求助
Hey there, let's troubleshoot this drag-and-drop headache you're having with Selenium 3.8 and Java! It's so frustrating when you can select the source element but can't finish dragging it to the target—let's break down the most likely fixes, especially since I notice your elements use Ember (those ember IDs are a dead giveaway!).
First, Quick Sanity Check
Your destination XPath looks truncated: //tbody[@class ='lt-body']//tr[@data-test-id='table-row-id-3... — make sure you finish this selector so Selenium can actually find the target element. That's an easy fix that's easy to overlook!
Solution 1: Refine Your Element Locators
You're targeting an <ellipse> inside an SVG for your source element. More often than not, SVG child elements aren't the ones that actually handle drag-and-drop events—instead, the parent container (like the <div> with the ember ID, or even the <tr> itself) is the interactive element. Try updating your source XPath to target a higher-level container, for example:
WebElement source = driver.findElement(By.xpath("//tbody[@class='lt-body']//tr[@data-test-id='table-row-id-20']"));
Or the div wrapping the SVG:
WebElement source = driver.findElement(By.xpath("//tbody[@class='lt-body']//tr[@data-test-id='table-row-id-20']//div[contains(@id,'ember')]"));
Solution 2: Use a More Explicit Actions Sequence
The built-in dragAndDrop() method can fail with complex frontends because it skips subtle mouse events that modern apps expect. Try manually simulating the full drag sequence with short pauses to mimic human behavior:
WebDriverWait wait = new WebDriverWait(driver, 10); // Wait for elements to be fully interactive WebElement source = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("YOUR_FIXED_SOURCE_XPATH"))); WebElement destination = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("YOUR_COMPLETE_DESTINATION_XPATH"))); Actions actions = new Actions(driver); // Simulate click-hold → move → pause → release actions.clickAndHold(source) .pause(500) // Short pause to mimic holding the element .moveToElement(destination) .pause(300) // Pause over target before releasing .release(destination) // Explicitly release on the target .build() .perform();
Solution 3: Fall Back to JavaScript Execution
If Selenium's Actions class still doesn't work (common with frameworks that override native drag events), use JavaScript to directly trigger the full chain of drag-and-drop events:
WebElement source = driver.findElement(By.xpath("YOUR_FIXED_SOURCE_XPATH")); WebElement destination = driver.findElement(By.xpath("YOUR_COMPLETE_DESTINATION_XPATH")); // JavaScript to simulate the full drag event lifecycle String dragScript = "function dragDrop(source, target) { " + "const dragStart = new MouseEvent('dragstart', {bubbles: true, cancelable: true}); " + "source.dispatchEvent(dragStart); " + "const dragOver = new MouseEvent('dragover', {bubbles: true, cancelable: true}); " + "target.dispatchEvent(dragOver); " + "const drop = new MouseEvent('drop', {bubbles: true, cancelable: true}); " + "target.dispatchEvent(drop); " + "const dragEnd = new MouseEvent('dragend', {bubbles: true, cancelable: true}); " + "source.dispatchEvent(dragEnd); " + "} " + "dragDrop(arguments[0], arguments[1]);"; ((JavascriptExecutor) driver).executeScript(dragScript, source, destination);
Solution 4: Rule Out Obscuring Elements or Iframes
- Double-check that no modals, overlays, or fixed headers/footers are blocking the destination element. Use
isDisplayed()andisEnabled()to verify interactivity. - If your elements live inside an iframe, switch to the iframe context first with
driver.switchTo().frame("frameIdOrName");before locating elements.
内容的提问来源于stack exchange,提问作者user9218893

