0

I press a button after a webpage loads into my WebBrowser control. How do I know it's loaded? I wait a significant amount of time to be sure.

Here is one version of the button code :

        var elementsx = webBrowser1.Document.GetElementsByTagName("input");
        foreach (HtmlElement file in elementsx)
        {
            if (file.GetAttribute("type") == "file")
            {
                listBox1.Items.Add(file.Style.ToString());
                file.Focus();
                file.InvokeMember("Click");
                SendKeys.Send(@"C:\Users\John\Desktop\test1\blue-book-motorcycle.jpg" + "{ENTER}");
            }
        }   

Here is another:

        mshtml.HTMLDocument doc = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
        // ################################################################################
        //get the title textbox
        IHTMLElementCollection inputer = (IHTMLElementCollection)doc.getElementsByTagName(@"input");
        foreach (IHTMLElement element in inputer) 
        {

            listBox1.Items.Add(element.style.cssText + " ||| " + element.getAttribute("type").ToString() + "%%%" + element.className);
            //we also get other textboxes with similar class names that begin with 
            //gwt-TextBox so we test for it.
            if(element.style.cssText=="height: 0px; visibility: hidden; position: absolute;" && 
               element.getAttribute("type").ToString()=="file")
            {
                ++i;
                if(i==3)
                {
                    element.click();
                    SendKeys.Send(@"C:\Users\John\Desktop\test1\blue-book-motorcycle.jpg" + "{ENTER}");
                }
            }
            //webBrowser1.Update();
        }

They both fail in the sense that, even though the open dialog appears , no keys are entered into the open dialog text box and it just sits there doing nothing.

Also, if I click the CANCEL button into the opendialog I get the following error: ":\Users\john\Desktop\test1\blue-book-motorcycle.jpg The filename is not valid" . So, the opendialog textbox stripped the "C" from my keys. What's the problem? I ran it as admin also. Fail!!!

kawa
  • 422
  • 4
  • 16

1 Answers1

0

You are failing on that because you are trying to put text into a modal dialog from the thread of your WebBrowser's Form. You won't succeed by doing this. If you want to fill the File Name on that dialog and press the Enter key, you need to create another thread and then you send the keys you need.

Try something like this:

    private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var elementsx = webBrowser1.Document.GetElementsByTagName("input");

        foreach (HtmlElement file in elementsx)
        {
            if (file.GetAttribute("type") == "file")
            {
                listBox1.Items.Add(file.Style.ToString());
                file.Focus();
                file.InvokeMember("Click");
                Task.Delay(500).ContinueWith(t => SendFileName(@"C:\Users\John\Desktop\test1\blue-book-motorcycle.jpg"), TaskScheduler.FromCurrentSynchronizationContext());
            }
        }
    }

    private void SendFileName(string fileName)
    {
        SendKeys.Send(fileName + "{ENTER}");
    }
Ismael
  • 622
  • 6
  • 11