Webdriver: File Upload

Viewed 48539

Is there a way to interact with a File Upload box in webdriver? The form field where the path gets put in is read only so I can't write to that.

7 Answers

I am in search of 3rd party libraries too,

Unless there is no any other Window Then this works for me :

in C# add reference for System.Windows.Forms

using System.Windows.Forms;

string url = "http://nervgh.github.io/pages/angular-file-upload/examples/image-preview/";
string path = @"C:\Users\File_Path";
IWebDriver d = new ChromeDriver();
d.Navigate().GoToUrl(url);
d.FindElement(By.XPath("//input[@type='file']")).Click();
hread.Sleep(5000);
System.Windows.Forms.SendKeys.SendWait(path);
System.Windows.Forms.SendKeys.SendWait(@"{Enter}");

in my case i can upload file like solution that write hear but dialog window stuck the process, the driver cant reference to close this window, so i kill him manually:

 foreach (var p in Process.GetProcessesByName("chrome"))
        if (p.MainWindowTitle.ToLower().Contains("open"))
               p.Kill();
Related