12

Is it possible to automate drag and drop from a file in system to a website in selenium using TestNG? Same as we do in Gmail where we can send attachment by drag and drop operation? please suggest.

  • Yes you can do it using the following command: Actions act = new Actions(driver); act.clickAndHold(webelement).perform(); act.MoveToElement(element).Release(); FYI: Draganddrop(element1,element2) command was available in previous versions of selenium but not in newer versions. – Sandeep Aug 23 '16 at 9:22
  • i have to upload the file from the desktop. how clickAndHold will work with that?? – iAutomate Aug 28 '16 at 15:01
15

Yes it's possible to drop a file with Selenium via a script injection. The script needs to create an <input> web element to receive the file sent by SendKeys. Then simulate the dragenter, dragover and drop events on the targeted element with the file set in the dataTransfer object.

This example drops an image from the file system on a drop area:

ChromeDriver driver = new ChromeDriver();

driver.get("http://html5demos.com/file-api");

// locate the drop area
WebElement droparea = driver.findElement(By.cssSelector("#holder"));

// drop the file
DropFile(new File("C:\\Downloads\\image.png"), droparea, 0, 0);

The DropFile method:

public static void DropFile(File filePath, WebElement target, int offsetX, int offsetY) {
    if(!filePath.exists())
        throw new WebDriverException("File not found: " + filePath.toString());

    WebDriver driver = ((RemoteWebElement)target).getWrappedDriver();
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    WebDriverWait wait = new WebDriverWait(driver, 30);

    String JS_DROP_FILE =
        "var target = arguments[0]," +
        "    offsetX = arguments[1]," +
        "    offsetY = arguments[2]," +
        "    document = target.ownerDocument || document," +
        "    window = document.defaultView || window;" +
        "" +
        "var input = document.createElement('INPUT');" +
        "input.type = 'file';" +
        "input.style.display = 'none';" +
        "input.onchange = function () {" +
        "  var rect = target.getBoundingClientRect()," +
        "      x = rect.left + (offsetX || (rect.width >> 1))," +
        "      y = rect.top + (offsetY || (rect.height >> 1))," +
        "      dataTransfer = { files: this.files };" +
        "" +
        "  ['dragenter', 'dragover', 'drop'].forEach(function (name) {" +
        "    var evt = document.createEvent('MouseEvent');" +
        "    evt.initMouseEvent(name, !0, !0, window, 0, 0, 0, x, y, !1, !1, !1, !1, 0, null);" +
        "    evt.dataTransfer = dataTransfer;" +
        "    target.dispatchEvent(evt);" +
        "  });" +
        "" +
        "  setTimeout(function () { document.body.removeChild(input); }, 25);" +
        "};" +
        "document.body.appendChild(input);" +
        "return input;";

    WebElement input =  (WebElement)jse.executeScript(JS_DROP_FILE, target, offsetX, offsetY);
    input.sendKeys(filePath.getAbsoluteFile().toString());
    wait.until(ExpectedConditions.stalenessOf(input));
}
1

You can use AutoIT it is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate task.

You can upload multiple files.

Please refer This

Hope it helps

Thanks

0

No, you cannot do it. Selenium only interacts with HTML element.

You can handle uploading file case by using below code

driver = webdriver.Firefox()

element = driver.find_element_by_id("fileUpload")

element.send_keys("C:\myfile.txt")

p/s: I copy this code from reference link below

Reference: How can I work with file uploads during a Webdriver test?

  • I have tried the above code.it gives me the following Exception. org.openqa.selenium.WebDriverException: unknown error: cannot focus element – iAutomate Aug 25 '16 at 4:05
  • The above code will only work when upload/browse button displayed with a text field else the above code will not work – Sandeep Aug 29 '16 at 8:02
  • the above code is for file upload with selenium and python,not for selenium+java. – Siv P Jul 17 '18 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.