上传文件不起作用 - 需要帮助 [英] Uploading Files Not Working - Need Assistance

查看:18
本文介绍了上传文件不起作用 - 需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WebBrowser 控件上传文件(图像).似乎无法做到,需要一些帮助.

I am trying to upload a file (an image) using the WebBrowser control. Can't seem to do it and need some help.

这是HTML:

<form action="https://post.testsite.com/k/IOkurrwY4xGI_EJMbjF5pg/zMNsR" method="post" enctype="multipart/form-data">
    <input type="hidden" name="cryptedStepCheck" value="U2FsdGVkX18yNzEwMjcxMJdrv2IidjtpGSCPzHNblWk02eJAJ6DFXFXic-Am1lTPMYL7k7XDoH0">
    <input type="hidden" name="a" value="add">
    <input class="file" type="file" name="file" multiple="multiple">
    <button class="add" type="submit" name="go"  value="add image">add image</button>
</form>

这里是 C# 代码...

Here is the C# code...

        elements = webBrowser.Document.GetElementsByTagName("input");
        foreach (HtmlElement file in elements)
        {
            if (file.GetAttribute("name") == "file")
            {
                file.Focus();
                file.InvokeMember("Click");
                SendKeys.Send("C:\Images\CCPhotoID.jpg" + "{ENTER}");
            }
        }

注意文件上传按钮出现,但不能在文件名区输入任何文件名.

Note that the file upload button comes up, but can't input any filename into the file name area.

推荐答案

IMO,您尝试做的确实是 UI 测试自动化的合法场景.IIRC,在 IE 的早期版本中,可以使用文件名填充 <input type="file"/> 字段,而不显示 Choose File对话.出于安全原因,这不再可能,因此您必须将密钥发送到对话框.

IMO, what you're trying to do is indeed a legit scenario for UI testing automation. IIRC, in early versions of IE it was possible to populate the <input type="file"/> field with a file name, without showing up the Choose File dialog. For security reason, this is no longer possible, so you have to send keys to the dialog.

这里的问题是 file.InvokeMember("Click") 显示了一个模态对话框,并且您希望将密钥发送到 那个 对话框,但是 SendKeys.Send 在对话框关闭后 被执行(毕竟它是模态的).您需要先让对话框打开,然后发送密钥并让它关闭.

The problem here is that file.InvokeMember("Click") shows a modal dialog, and you want the keys to be sent to that dialog, but SendKeys.Send gets executed after the dialog has been closed (it's modal, after-all). You need to let the dialog open first, then send the keys and let it close.

这个问题可以使用 WinForms Timer 解决,但我更喜欢使用 async/awaitTask.Delay 来解决这个问题(工作代码):

This problem can be solved using WinForms Timer, but I'd prefer using async/await and Task.Delay for this (working code):

async Task PopulateInputFile(HtmlElement file)
{
    file.Focus();

    // delay the execution of SendKey to let the Choose File dialog show up
    var sendKeyTask = Task.Delay(500).ContinueWith((_) =>
    {
        // this gets executed when the dialog is visible
        SendKeys.Send("C:\Images\CCPhotoID.jpg" + "{ENTER}");
    }, TaskScheduler.FromCurrentSynchronizationContext());

    file.InvokeMember("Click"); // this shows up the dialog

    await sendKeyTask;

    // delay continuation to let the Choose File dialog hide
    await Task.Delay(500); 
}

async Task Populate()
{
    var elements = webBrowser.Document.GetElementsByTagName("input");
    foreach (HtmlElement file in elements)
    {
        if (file.GetAttribute("name") == "file")
        {
            file.Focus();
            await PopulateInputFile(file);
        }
    }
}

IMO,这种方法对于 UI 自动化脚本非常方便.您可以像这样调用 Populate,例如:

IMO, this approach is very convenient for UI automation scripts. You can call Populate like this, for example:

void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    this.webBrowser.DocumentCompleted -= webBrowser_DocumentCompleted;
    Populate().ContinueWith((_) =>
    {
        MessageBox.Show("Form populated!");
    }, TaskScheduler.FromCurrentSynchronizationContext());
}

这篇关于上传文件不起作用 - 需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆