上传文件不工作 - 需要帮助 [英] Uploading Files Not Working - Need Assistance
问题描述
我要上传使用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#code ...
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.
推荐答案
海事组织,你想要做的是确实是一个合法的场景UI测试自动化。 IIRC,在IE浏览器的早期版本有可能填充&LT;输入类型=文件/&GT;
字段的文件名,而不显示了选择文件
对话框。出于安全原因,这已不再是可能的,所以你必须键发送到对话框。
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(点击)
显示一个模式对话框,并且希望的钥匙,被送到的是的对话已关闭>对话框,但 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 定时器
来解决,但我用异步/计谋
倒是preFER和 Task.Delay
这个(工作code):
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自动化脚本非常方便。您可以拨打填充
这样的,例如:
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser.DocumentCompleted -= webBrowser_DocumentCompleted;
Populate().ContinueWith((_) =>
{
MessageBox.Show("Form populated!");
}, TaskScheduler.FromCurrentSynchronizationContext());
}
这篇关于上传文件不工作 - 需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!