Webbrowser with input type file

4

Friends, I'm building a virtual robot to work on a particular site.

In a certain action I need to upload a file to the site, but when the robot clicks the <input type="file"> button it logically opens a modal window of Windows Explorer. Can someone help me CLOSE this window? Well, I'm already able to upload, but the robot does not continue until I close the window ...

<input type="file" name="uploadFile" size="80" tabindex="0" value="Procurar..." style="width:340px">
        HtmlElement fieldset = doc.GetElementsByTagName("fieldset")[4];
        HtmlElement input_ImpArq = fieldset.GetElementsByTagName("INPUT")[0];
        input_ImpArq.InvokeMember("Click");                                  

        string caminho = "\C:\9000144.txt";
        SendKeys.Send(caminho + "{ENTER}");

        HtmlElement formulario = doc.GetElementById("formBusca");
        formulario.InvokeMember("submit");
    
asked by anonymous 16.03.2015 / 13:08

1 answer

2

I was able to accomplish what you need with help in Stack Overflow in English. The secret is to use Task with Delay :

Create an asynchronous task to grab the controls and upload:

Task.Run(async () =>
{
    HtmlElement fieldset = doc.GetElementsByTagName("fieldset")[4];
    HtmlElement input_ImpArq = fieldset.GetElementsByTagName("INPUT")[0];
    await Upload(input_ImpArq);
}).ContinueWith(x =>
 {
     HtmlElement formulario = webBrowser.Document.GetElementById("formBusca");
     formulario.InvokeMember("submit");
 }, TaskScheduler.FromCurrentSynchronizationContext());

Here is the code of the uploading method:

async Task Upload(HtmlElement inputFile)
{
    inputFile.Focus();

    // atrasa a execução do ENTER para que a janela de seleçãp apareça
    var sendKeyTask = Task.Delay(500).ContinueWith((_) =>
    {
        // isso é executado quando a janela está visível 
        SendKeys.Send(@"D:\teste.txt" + "{ENTER}");
    }, TaskScheduler.FromCurrentSynchronizationContext());

    inputFile.InvokeMember("Click");

    await sendKeyTask;

    // aqui está o segredo, aguarda a janela de seleção de arquivp fechar
    await Task.Delay(500);
}

You can get more information about asynchronous execution of Task on:

01.05.2015 / 16:04