5

What I'm working with:

To start off, my HTML looks the following:

<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 posted the entire thing, the file input is the following part:

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

My situation:

I'm using Selenium WebDriver with PhantomJs (C#). Before this I used FireFox instead of PhantomJs and uploaded a file the following way:

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

My issue:

However this crashes when I use PhantomJs.

My attempt to solve the issue:

I found the following thread 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, while this doesn't throw an error it doesn't upload the file.

After having searched far and wide my only option is to ask here, hoping someone can help me out.

It seems to me that this is a common problem for many people using PhantomJs+Selenium and it makes me wonder why this bug has yet to be fixed.

JensOlsen112
  • 1,279
  • 3
  • 20
  • 26
  • How does it crash in case of `SendKeys()` approach? – alecxe Apr 03 '15 at 20:17
  • @alecxe I get the message: "The HTTP request to the remote WebDriver server for URL http://localhost:9648/session/c05b78a0-da4b-11e4-955d-1502c51c62ed/element/:wdc:1428097984752/value timed out after 60 seconds." – JensOlsen112 Apr 03 '15 at 21:55
  • I am facing the same situation - PhantomJS hangs on Sendkeys which is a known issue and the executePhantomJS function does not give me any error, but does not actually upload the file. Have you tried the same script but with HtmlUnitDriver? – LittlePanda Apr 05 '15 at 14:28
  • I have posted this same question. No one has answered and I am thinking of trying out javascript+phantomjs in Nodeclipse - http://stackoverflow.com/questions/29315319/selenium-ghostdriver-phantomjs-file-not-getting-uploaded-using-sendkeys-jav – LittlePanda Apr 05 '15 at 14:37
  • @LittlePanda I've tried it but I don't know how to get HtmlUnitDriver working. I have the same issue as this unresolved question: http://stackoverflow.com/questions/29456504/running-htmlunit-in-c – JensOlsen112 Apr 05 '15 at 19:48
  • @JensOlsen112: It's alright. HtmlUnitDriver is easier to setup with Selenium but it does not identify elements properly. PhantomJS is better in that sense. I will definitely post an answer if I find any solution. Favourited this question! – LittlePanda Apr 06 '15 at 03:38
  • @LittlePanda Thanks, please do :) – JensOlsen112 Apr 06 '15 at 17:57

2 Answers2

1

Just faced the same issue today and resolved it. File upload functionality is broken in PhantomJS 2.0. Please check this thread https://github.com/ariya/phantomjs/issues/12506 for more information.

So generally you should make a custom build of PhantomJS browser (or wait for official fix) and use the script already mentioned above:

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

Please note that some custom builds are already available in the thread (I took a custom build for Windows there).

0

Take a look at my answer here It goes over the process I have had luck with in the past when dealing with non-browser dialog boxes (such as file uploads).

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

Community
  • 1
  • 1
aholt
  • 2,829
  • 2
  • 10
  • 13
  • Thanks for the answer. Will definitely try this out! – LittlePanda Apr 07 '15 at 08:06
  • Thanks for the suggestion. The issue is that this solution sends a "raw" web request, meaning that I have to manually create a new method/inspect the form for each individual file upload. I wish there was a way to do it as simple as using SendKeys(). – JensOlsen112 Apr 30 '15 at 16:13
  • yeah, there really isn't a silver-bullet easy button for file dialogs in selenium. – aholt Apr 30 '15 at 16:47