Webbrowser with input type file

Asked

Viewed 495 times

4

Friends, I am building a virtual robot to act on a particular site.

In a given action I need to upload a file to the site, but when the robot clicks on the button <input type="file"> logically opens a modal window of Windows Explorer. Can someone help me close this window? 'Cause I’m already uploading, but the robot won’t 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");
  • When you choose the file the window does not automatically close?

1 answer

2

I managed to accomplish what you need with help of that question in the Stack Overflow in English. The secret lies in the use of 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 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 in:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.