Drag and drop function is not working in chromedriver using selenium webdriver and Node js for Test automation

Viewed 1415

Is there a way where I can make drag and drop to in selenium using Node.js? I am using the function shown below but it does not seem to work.

driver.actions().dragAndDrop(source,destination).perform())
3 Answers

First of all, you forgot the build() method.
Second of all, inspect the html code and find if your drag&drop is into Iframe tag.
If it is, then you need to switch to that Iframe.

So:

driver.switchTo().frame(driver.findElement(By.xpath("PutYourXpathIframe")));  

Actions a = new Actions(driver);
WebElement source = driver.findElement(By.id("PutYourSourceId"));
WebElement target = driver.findElement(By.id("PutyourTargerId"));
a.dragAndDrop(source,target).build().perform();

In the end you may want to switch back to default content:

driver.switchTo().defaultContent();

As a workaround could you use the following?:

 browser.actions()
    .mouseDown(element1) //optionally
    .mouseMove(source)
    .mouseMove(destination)
    .mouseUp()
    .perform();

This page has a good example of how to do this.

var assert = require(‘assert’);
var webdriver = require(‘selenium-webdriver’),
By = webdriver.By,
until = webdriver.until;

var chrome = require(“selenium-webdriver/chrome”);

var options = new chrome.Options();
options.addArguments(“start-maximized”);
options.addArguments(“disable-popup-blocking”);
options.addArguments(“test-type”); 
var driver = new webdriver.Builder().
withCapabilities(options.toCapabilities()).build();

driver.get(‘http://www.softpost.org/selenium-test-page/’);
var e3 = driver.findElement(By.tagName(‘select’));

//You can use ActionSequence class to perform actions in selenium

new webdriver.ActionSequence(driver).
keyDown(webdriver.Key.SHIFT).
click(e3).
//dragAndDrop(element3, element4).
keyUp(webdriver.Key.SHIFT).
perform();

//In the same way, you can perform below actions.
//sendKeys, mouseUp , mouseMove, mouseDown, dragAndDrop, doubleClick

driver.sleep(5000);

driver.quit();
//Similarly we can also do touch actions on mobile phones`
Related