Why drag and drop is not working in Selenium Webdriver?

后端 未结 12 1661
借酒劲吻你
借酒劲吻你 2021-01-11 11:45

I am trying to drag an element into another element using Selenium WebDriver but it\'s not working. I tried all the solutions which I can find on internet but none of the so

相关标签:
12条回答
  • 2021-01-11 11:52

    Can you try Java Script Executor for this

    JavascriptExecutor js = (JavascriptExecutor)driver
    js.executeScript("function createEvent(typeOfEvent) {\n" + "var event =document.createEvent(\"CustomEvent\");\n"
                        + "event.initCustomEvent(typeOfEvent,true, true, null);\n" + "event.dataTransfer = {\n" + "data: {},\n"
                        + "setData: function (key, value) {\n" + "this.data[key] = value;\n" + "},\n"
                        + "getData: function (key) {\n" + "return this.data[key];\n" + "}\n" + "};\n" + "return event;\n"
                        + "}\n" + "\n" + "function dispatchEvent(element, event,transferData) {\n"
                        + "if (transferData !== undefined) {\n" + "event.dataTransfer = transferData;\n" + "}\n"
                        + "if (element.dispatchEvent) {\n" + "element.dispatchEvent(event);\n"
                        + "} else if (element.fireEvent) {\n" + "element.fireEvent(\"on\" + event.type, event);\n" + "}\n"
                        + "}\n" + "\n" + "function simulateHTML5DragAndDrop(element, destination) {\n"
                        + "var dragStartEvent =createEvent('dragstart');\n" + "dispatchEvent(element, dragStartEvent);\n"
                        + "var dropEvent = createEvent('drop');\n"
                        + "dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n"
                        + "var dragEndEvent = createEvent('dragend');\n"
                        + "dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);\n" + "}\n" + "\n"
                        + "var source = arguments[0];\n" + "var destination = arguments[1];\n"
                        + "simulateHTML5DragAndDrop(source,destination);", ElementFrom, ElementTo);
    

    Refrence: https://www.linkedin.com/pulse/javascriptexecutor-selenium-gaurav-gupta/

    It works for me on web application which are angular based.

    0 讨论(0)
  • 2021-01-11 11:52

    This is working for me :

    Actions act = new Actions(driver);
    act.moveToElement(element, (elementWidth / 2), elementHeight / 2).clickAndHold().build().perform();
    act.moveToElement(dest, (destWidth / 2) , (destHeight / 2)).release().build().perform();
    

    There are sometimes bugs in some version of selenium. Make sure you use the lastest one and play around with clicking. Would be easier of you can send a link of what you are trying to drag/drop

    0 讨论(0)
  • 2021-01-11 11:54

    I have also faced the same issue. Please find below custom java-script function for drag & drop.

    1) Create DragDrop.js file and paste below code in it

        function customEvent(typeOfEvent) {
        var event = document.createEvent("CustomEvent");
        event.initCustomEvent(typeOfEvent, true, true, null);
        event.dataTransfer = {
            data: {},
            setData: function (key, value) {
                this.data[key] = value;
            },
            getData: function (key) {
                return this.data[key];
            }
        };
        return event;
    }
    function dispatchEvent(element, event, transferData) {
        if (transferData !== undefined) {
            event.dataTransfer = transferData;
        }
        if (element.dispatchEvent) {
            element.dispatchEvent(event);
        } else if (element.fireEvent) {
            element.fireEvent("on" + event.type, event);
        }
    }
    function executeDrageAndDrop(element, target) {
        var dragStartEvent = customEvent('dragstart');
        dispatchEvent(element, dragStartEvent);
        var dropEvent = customEvent('drop');
        dispatchEvent(target, dropEvent, dragStartEvent.dataTransfer);
        var dragEndEvent = customEvent('dragend');
        dispatchEvent(element, dragEndEvent, dropEvent.dataTransfer);
    }
    

    2) Using below code we can call above custom function(Below is C# code)

    string script = System.IO.File.ReadAllText(@"{filepath of DragDrop.js file}");
    script = script + "executeDrageAndDrop(arguments[0], arguments[1])";
    IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
    
    IWebElement source = driver.findElement(By......);
    IWebElement target = driver.findElement(By......);
    
    executor.ExecuteScript(script, source, target);
    
    0 讨论(0)
  • 2021-01-11 11:55

    I've tried a bunch of workarounds, this one seems to work for me using macOS and chromedriver

    public void dragAndDrop(WebElement source, WebElement target) throws AWTException {
        new Actions(driver).dragAndDrop(source, target).release().build().perform();
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ESCAPE);
        robot.keyRelease(KeyEvent.VK_ESCAPE);
    }
    
    0 讨论(0)
  • 2021-01-11 11:57

    If the known cases do not work, you can try this solution

            WebElement a = driver.findElement(By.cssSelector("your_selector"));
            WebElement b = driver.findElement(By.cssSelector("your_selector"));
    
            int x = b.getLocation().x;
            int y = b.getLocation().y;
    
            Actions actions = new Actions(driver);
            actions.moveToElement(a)
                    .pause(Duration.ofSeconds(1))
                    .clickAndHold(a)
                    .pause(Duration.ofSeconds(1))
                    .moveByOffset(x, y)
                    .moveToElement(b)
                    .moveByOffset(x,y)
                    .pause(Duration.ofSeconds(1))
                    .release().build().perform();
    
    0 讨论(0)
  • 2021-01-11 12:00

    In your code 1: Not calling perform() method, it should be

     Actions builder = new Actions( _controls.getDriver());
    
     builder.dragAndDrop(sourceelement, destelement).perform();
    

    In your code 2: I don't think you need to call release()

    Please search for similar questions before posting.

    0 讨论(0)
提交回复
热议问题