-->

WatiN File Upload

2019-07-17 04:14发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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";


回答4:

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.


标签: c# .net watin