WatiN File Upload

2019-07-17 03:59发布

I've hit a snag. I am trying to use WatiN to upload a file. I can load the upload box, but it quickly disappears. The last line of my code at the moment is:

ie.FileUpload(Find.ById("profile_file")).Click();

It loads the dialog to select a picture but disappears. Is it possible to set the path of the box automatically example, load "C:/Desktop/image.jpg"?

Also, is it possible to wait for the upload to complete before continuing?

Help is much appreciated.

Thanks.

标签: c# .net watin
4条回答
倾城 Initia
2楼-- · 2019-07-17 04:39

While this won't answer your question directly, I suspect there is a DialogWatcher automatically canceling the dialog.

查看更多
疯言疯语
3楼-- · 2019-07-17 04:42

As a security measure, browsers don't allow scripts to set the value of a file input. Imagine if they did, I could put a hidden file input on my page where I have a contact form, and set its value to c:\topsecretinfo.txt without the user's permission. then when the user submits the form, voilà, I'd have their top secret information.

As luck has it, I ran into this issue today and this is what I did to get around it:

  • created a new hidden field on the page called "testupload".
  • used Watin to set the value of this hidden field to the path to my test file
  • on the server side, I check for this field and if it exists, instead of reading the posted file, I read this file from my local machine.
查看更多
一夜七次
4楼-- · 2019-07-17 04:52

This is 6 years late, but if anyone else (like me) is still having trouble with this, here's a solution.

To fill out the file dialogue, try this:

ie.FileUpload(Find.ById("profile_file")).Set("filename.txt");

This will automatically pull up the file dialogue. You'll then see text being automatically typed into the file selection textfield.

No need for any sendkeys.

查看更多
可以哭但决不认输i
5楼-- · 2019-07-17 04:53

why do you need to select from the Dialog.. Try to just sent the Text:

ie.FileUpload(Find.ById("profile_file")).Text = "C:/Desktop/image.jpg";
查看更多
登录 后发表回答