5

May be this question is asked in past but I have searched and not found its solution yet.

I have tried all the options that I have found till now but all in vain. SendKeys doesn't work as it does not fill the file input box with file path, that is to be uploaded. Cannot set file input box "SetAttribute" value as there is no value attribute available:

thats all.

If I use element.focus() it pops up "choose file to upload" dialog and now I don't know how to fill it programmatically and open it in file input box.

I want it to be automated completed so that user does not have to interact with the application. Application shall pick the file from hard disk from given file path and fill other fields of form then start uploading, all using webbrowser control in windows form application.

No solutions found!

Can anyone help please? (This is my first ever question on stackoverflow, therefore if I am doing anything wrong then please guide, I mean if I am not allowed to post such question!)

Here is the code:

        HtmlElementCollection heCollection = doc.GetElementsByTagName("input");
        foreach (HtmlElement heSpan in heCollection)
        {
          string strType = heSpan.GetAttribute("type");
          string strName = heSpan.GetAttribute("name");
          if (strType.Equals("file") && strName.Equals("file"))
          {
            heSpan.Focus();
            //heSpan.SetAttribute("value", "test.jpg");
            SendKeys.Send("C:\\1.txt");
            //heSpan.InnerText = "c:\\1.txt";
          }

          //Title for the attachment
          if (strName.Equals("field_title"))
          {
            heSpan.InnerText = "1.txt";
          }
        }

When this code executes, cursor starts blinking in fine input box (as I have set heSpan.focus()) but the file path doesn't show in the file input box. If I implement heSpan.InvokeMember("click"); It opens the choose a file to upload dialoge/popup window and there I get stuck, because I don't know how to fill that popup dynamically and then insert the file path in file input box.

leppie
  • 115,091
  • 17
  • 196
  • 297
Yaaver
  • 51
  • 1
  • 4
  • Since this is your first question, let me offer you some advice. I would explain your question in the body of the question and not just in the title. If you rely only on the title, your actual question might get lost. I'd also suggest you show some code to illustrate what you have tried and why it isn't working. – psubsee2003 Feb 02 '13 at 18:08
  • I have added the code and have explained a bit about what I have tried. May be now its quite clear :/ – Yaaver Feb 02 '13 at 19:13
  • See this one for example: http://stackoverflow.com/questions/6962931/how-to-intercept-webbrowser-controls-choose-file-to-upload-window-before-it-o – CodeCaster Apr 11 '13 at 06:36
  • A similar question [answered](http://stackoverflow.com/a/18691219/1768303). – noseratio Sep 16 '13 at 05:42

1 Answers1

0

Try setting the focus to the WebBrowser control right before you set the focus to the input field. That worked for me.

user6
  • 1,999
  • 2
  • 23
  • 29