[C#] Automate Open File Dialog, AutomationElement?

Soldato
Joined
6 Jan 2005
Posts
3,633
Location
Cambridge
Hi, I'm currently trying to make a program that opens a web page, and uploads a file to the webserver. The web page has a captcha on it to stop bots just uploading loads of files, so I can't simply send the file through "post" or whatever it's called.

I've got to a point where I'm stuck and hopefully I can find help from you bunch of great coders.

I open the open file dialog from the web page with:
Code:
HtmlDocument doc = this.webBrowser1.Document;
doc.Body.All["file"].InvokeMember("click");

Next I need to use AutomationElement(I think) to input the file name and click Open in the open file dialog.
Hopefully the dialog can be hidden aswell?

If someone could help me with that, any help would be greatly appreciated.

Thanks,
yhack
 
Last edited:
Can you not just set the value for the "file" field?

(This is a complete guess)

I tried that but it doesn't seem to work.

Here's the code I tried:
Code:
HtmlDocument doc = this.webBrowser1.Document;
doc.Body.All["file"].SetAttribute("value", "C:\whatever.txt");

It works fine for text fields but for these it doesn't.
 
Last edited:
What about sValue:

Code:
HtmlDocument doc = this.webBrowser1.Document;
doc.Body.All["file"].SetAttribute("sValue", "C:\whatever.txt");

Again, complete guess

After a search, turns out you can't change the value, otherwise you'd be able to make web pages with javascript that downloads any file from your computer without permission. Thanks for your ideas though :)

I think the only way is with AutomationElement, but I'm having trouble finding any good tutorials for it.
 
How do you propose to get around the captcha?

Once you've worked that out, you don't need to worry about any open file dialogs or anything, just parse the response from the server, then generate a request with your file in it.
 
How do you propose to get around the captcha?

Once you've worked that out, you don't need to worry about any open file dialogs or anything, just parse the response from the server, then generate a request with your file in it.

It's easy to get the captcha image from the webbrowser by searching for the image url in the source.

Here's how I do it:
Code:
string pageSource = webBrowser1.Document.Body.InnerHtml; //Source
string captchaUrl = GetStringInBetween("captchaimg\" src=\"", "\">", pageSource, false, false); //Get Url
pictureBox1.ImageLocation = captchaUrl; //Show Picture

Here's GetStringInBetween if anyone wants it.
Code:
public static string GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)
        {
            string[] result = { string.Empty, string.Empty };
            int iIndexOfBegin = strSource.IndexOf(strBegin);

            if (iIndexOfBegin != -1)
            {
                // include the Begin string if desired 
                if (includeBegin)
                    iIndexOfBegin -= strBegin.Length;

                strSource = strSource.Substring(iIndexOfBegin + strBegin.Length);

                int iEnd = strSource.IndexOf(strEnd);
                if (iEnd != -1)
                {
                    // include the End string if desired 
                    if (includeEnd)
                        iEnd += strEnd.Length;
                    result[0] = strSource.Substring(0, iEnd);
                    // advance beyond this segment 
                    if (iEnd + strEnd.Length < strSource.Length)
                        result[1] = strSource.Substring(iEnd + strEnd.Length);
                }
            }
            else
                // stay where we are 
                result[1] = strSource;
            return result[0];
        }

Maybe I could edit the information sent in some way and add my own file.
 
Why are you writing your own web-browser?

Seriously, just parse the response, then form a request with your info.

In pseudo..

Make initial request for page.
Receive/Read response (i.e. the page)
Send new request with captcha and file.
 
Why are you writing your own web-browser
I'm not, I'm using the webbrowser tool in Visual Studio, it's like a Internet Explorer but stripped of everything.
Seriously, just parse the response, then form a request with your info.

In pseudo..

Make initial request for page.
Receive/Read response (i.e. the page)
Send new request with captcha and file.
Ok, I'll give that a go. The way in my above posts was the only way I could think of doing it, using the webbrowser

Thanks :)
 
Back
Top Bottom