Unable to upload file using PhantomJs (and Selenium WebDriver)

What I work with:

To get started, my HTML looks like this:

<form action="http://example.com/upload_photo_iframe.html" preview_div="upload_photo_div" submit_button="submit_btn" upload_field="photo_upload" target="photo_target" enctype="multipart/form-data" method="POST" id="uploadfile" name="uploadfile"> <input type="hidden" value="CSRF_iframe_photo_upload" name="csrfname"> <input type="hidden" value="3350427f0f068509081a09e283607214001b6912843ffb937b934208c91d9041c88faf0e66df4f3898ef202a34b669647f5b3fd9a2122e389acd3f53c33fc88b" name="csrftoken"> <label class="file-upload"> <p id="upload_text">Click Browse to choose a file then click Upload</p> <input type="file" class="file" id="photo_upload" name="photo_upload" accept="image/*"> </label> <input type="hidden" value="true" name="makeLargeThumb"> <input type="hidden" value="p" name="size"> <input type="hidden" value="P" name="type"> <input type="hidden" value="5120000" name="MAX_FILE_SIZE"> <input type="hidden" value="0" name="album_id" class="album_id_hidden"> <input type="submit" class="grey_btn" id="upload_btn" value="Upload" name="submit_btn"> <input type="hidden" name="callback" value="document.forms.uploadfile.handleReceive"><input type="hidden" name="fieldName" value="photo_upload"></form> 

I published everything, the input file is as follows:

 <input type="file" class="file" id="photo_upload" name="photo_upload" accept="image/*"> 

My situation:

I am using Selenium WebDriver with PhantomJs (C #). Before that, I used FireFox instead of PhantomJs and uploaded the file as follows:

 _driver.FindElement(photoUploadPath).SendKeys(imagePath); 

My problem:

However, this happens when I use PhantomJs.

My attempt to solve the problem:

I found the following topic discussing the issue: https://github.com/ariya/phantomjs/issues/10993

And I tried the proposed solution:

 ((PhantomJSDriver)driver).executePhantomJS("var page = this; page.uploadFile('input[type=file]', '/path/to/file');"); 

However, although this does not cause an error, it does not download the file.

After I searched all the way, my only option is to ask here, hoping that someone can help me.

It seems to me that this is a common problem for many people using PhantomJs + Selenium, and this makes me wonder why this error has not yet been fixed.

+5
source share
2 answers

Today I ran into the same problem and solved it. File upload functionality is broken in PhantomJS 2.0. Please see this topic https://github.com/ariya/phantomjs/issues/12506 for more information.

As a rule, you should make your own build of the PhantomJS browser (or wait for the official correction) and use the script mentioned above:

 ((PhantomJSDriver)driver).executePhantomJS("var page = this; page.uploadFile('input[type=file]', '/path/to/file');"); 

Please note that some custom assemblies are already available in the stream (I took a special assembly for Windows there).

+1
source

Take a look at my answer here . He moved on to the process with which I was lucky in the past when it comes to dialog boxes without a browser (for example, downloading files).

While the question I linked to you is about Java, the code in my answer is actually C #.

0
source

All Articles