Why drag and drop is not working in Selenium Webdriver?

后端 未结 12 1657
借酒劲吻你
借酒劲吻你 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 12:06

    I would suggest you try the following solution:

    WebElement sourceelement  = driver.findElement(By.cssSelector("XXX"));
    Locatable element = (Locatable)sourceelement ;
    Point p= element.getCoordinates().inViewPort();
    int sourceX=p.getX();
    int sourceY=p.getY();
    
    WebElement destelement = driver.findElement(By.cssSelector("YYY"));
    Locatable elementTarget = (Locatable)destelement;
    Point Target= elementTarget.getCoordinates().inViewPort();
    int targetX=Target.getX();
    int targetY=Target.getY();
    

    You may then use Robot to drag and drop the element

    0 讨论(0)
  • 2021-01-11 12:07

    You may try executing the following javascript to perform drag and drop

    WebDriver _driver;
    WebElement _sourceElement = _driver.findElement(<source>);
    WebElement _targetElement = _driver.findElement(<source>);
    JavascriptExecutor _js = (JavascriptExecutor) _driver;
    _js.executeScript("$(arguments[0]).simulate('drag-n-drop',{dragTarget:arguments[1],interpolation:{stepWidth:100,stepDelay:50}});", _sourceElement, _targetElement);
    

    Please find more details here.

    It works perfectly for all the browsers and devices.

    0 讨论(0)
  • 2021-01-11 12:09
    Actions act = new Actions(driver);
    WebElement source = driver.findElement(By.id("XXX"));
    WebElement destination = driver.findElement(By.id("XXX"));
    
    Action dragAndDrop =act.moveToElement(source,destination).build().perform();
    
    0 讨论(0)
  • 2021-01-11 12:13

    I have face similar problem way back, I have used dragAndDropBy to move slider but it didn't worked for me but later I found help and below the snippet for my working code:

    public static void slider(){
    x=10;
    WebElement slider = driver.findElement(By.id("slider"));
    int width=slider.getSize().getWidth();
    Actions move = new Actions(driver);
    move.moveToElement(slider, ((width*x)/100), 0).click();
    move.build().perform();
    System.out.println("Slider moved");
    }
    

    You can refer the link here

    0 讨论(0)
  • 2021-01-11 12:15

    You might want to check if webelement is enabled or displayed prior performing desired action over it. You may give it a try with below code

    public void dragAndDrop(WebElement sourceElement, WebElement destinationElement) {
        try {
            if (sourceElement.isDisplayed() && destinationElement.isDisplayed()) {
                Actions action = new Actions(driver);
                action.dragAndDrop(sourceElement, destinationElement).build().perform();
            } else {
                System.out.println("Element was not displayed to drag");
            }
        } catch (StaleElementReferenceException e) {
            System.out.println("Element with " + sourceElement + "or" + destinationElement + "is not attached to the page document "
                    + e.getStackTrace());
        } catch (NoSuchElementException e) {
            System.out.println("Element " + sourceElement + "or" + destinationElement + " was not found in DOM "+ e.getStackTrace());
        } catch (Exception e) {
            System.out.println("Error occurred while performing drag and drop operation "+ e.getStackTrace());
        }
    }
    
    
    public void dragAndDrop(WebElement sourceElement, WebElement destinationElement)
        {
            (new Actions(driver)).dragAndDrop(sourceElement, destinationElement).perform();
        }
    }
    
    0 讨论(0)
  • 2021-01-11 12:15

    I would suggest you to use Touch Action to perform drag and drop.

    Point coordinates1 = sourceelement.getLocation();
    Point coordinates2 = destelement.getLocation();  
    TouchActions builder = new TouchActions(driver);
    builder.longPress(coordinates1)
           .move(coordinates2).release(coordinates2).perform();
    
    0 讨论(0)
提交回复
热议问题