cashanzlo
cashanzlo

Reputation: 121

Automate drag & drop functionality [File Upload] using Selenium WebDriver in C#

I need to automate drag and drop functionality in C# but couldn't find the right way to do it! This is a file upload form and I need to automate drag and drop action there.

Experimented this way :

public void FileAttachment() {
    PageObjects.UploadFile.Click();
    Driver.Instance.FindElement(By.XPath("//*@id='fileUpload']div")).SendKeys("filepath");
    PageObjects.FileUploadSend.Click();
}

Help me getting through this rock!

Upvotes: 0

Views: 3780

Answers (2)

cashanzlo
cashanzlo

Reputation: 121

After few research works, I wish to share how I fixed it!

AutoIt is an automation tool like Selenium but unlike Selenium it is used for Desktop Automation rather Web Automation. It automates mouse movements & keystrokes, desktop windows, buttons & forms.

Installing it and Referencing into the project makes it happen!

So code looks like,

public void FileAttachment()
{
        PageObjects.UploadFile.Click();
        Driver.Instance.FindElement(By.XPath("//*@id='fileUpload']div")).Click();
        AutoItX3 autoIt = new AutoItX3();
        autoIt.WinActive("Open"); //Differs from Browser to Browser
        autoIt.Send(@"C:Desktop\doccs\trialTest.txt");
        autoIt.Send("{ENTER}");
        Thread.Sleep(1000);
        PageObjects.FileUploadSend.Click();
}

Upvotes: 4

Memfisto
Memfisto

Reputation: 352

Are you using HTML5 drag and drop? I am stuck with the same issue, and seeing from the various threads, using HTML5 drag and drop has been bugged since few years ago and doesn't seem to be fixed. I found some alternative ways from github achieves, they require javascript execution though, as well as translating the code to C# : http://elementalselenium.com/tips/39-drag-and-drop

Upvotes: 0

Related Questions