0

I have a win forms app and I'm opening a webBrowser inside the app. I have to use the browser, since I'm automating the actual browser UI flow of the site. I hit a snag trying to upload a file. The input element type is "file" and, as I've discovered, the only way to populate it programatically is to hit the browse button and select the actual file from the "Choose File to Upload" dialog.

I found useful answers on the topic like: Uploading Files Not Working - Need Assistance

I tried using the above solution, but my code doesn't move beyond file.InvokeMember("Click");. So basically, once the "Choose File to Upload" dialog opens, the code pauses and waits for the dialog to close. I'm not sure how to proceed from here. Would really appreciate help on this and maybe a better suggestion on dealing with <input type="file"..." elements.

Community
  • 1
  • 1
user3516946
  • 1
  • 1
  • 1
  • Did you try the next part from that answer? `SendKeys.Send("C:\\Images\\CCPhotoID.jpg" + "{ENTER}");` – Jon B Apr 09 '14 at 20:41
  • Yes, just like in that solution, tryng to send keys to populate and close dialog, but it only gets to that next like after I manually with my mouse close the dialog. – user3516946 Apr 09 '14 at 20:43
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 09 '14 at 20:44

1 Answers1

0

I hope the following answer will help you. Add timer control to your form. Before you invoke the click event to the upload file dialog, start the timer.

timer1.Interval = 3000;
timer1.Start();
file.InvokeMember("Click");

In the timer tick event add the following coding

    private void timer1_Tick(object sender, EventArgs e)
    {
        try
        {
            timer1.Stop();
            SendKeys.SendWait("D:\\testing.txt"); // enter the file path, which suppose to upload.
            SendKeys.SendWait("{TAB 2}");
            SendKeys.SendWait("{ENTER}");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
Golda
  • 3,823
  • 10
  • 34
  • 67