Question

I am trying to upload file using selenium web driver by the following code:

        WebDriver driver = new HtmlUnitDriver();

        // And now use this to visit login page
        driver.get(URL_UPLOADFORM);

        // Find the text input element by its name
        WebElement userIDElement = driver.findElement(By.id("user_login"));
        userIDElement.sendKeys(USER_ID);

        WebElement passwordElement=driver.findElement(By.id("user_password"));
        passwordElement.sendKeys(PASSWORD);

        passwordElement.submit();
        // Enter something to search for
        //element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element and redirect to the form with file upload page
        //element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        System.out.println(driver.getCurrentUrl());


        WebElement fileUpload=driver.findElement(By.id("gallery_item_photo"));
        System.out.println(fileUpload.toString());
        fileUpload.sendKeys("C:\\Users\\abc\\Pictures\\fileimg.jpg");
        WebElement commitButton=driver.findElement(By.name("commit"));
        System.out.println(commitButton.toString());
        commitButton.click();

        System.out.println(driver.getCurrentUrl());
        driver.quit();

The file is not getting uploaded and the output is:

Page title is: New Photo
http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items/new
<input id="gallery_item_photo" name="gallery_item[photo]" value="" type="file" />
<button class="big button" name="commit" type="submit" value="commit">
http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items

Here is the form element's html using which I am trying to upload file:

<form accept-charset="UTF-8" action="/projects/276345/gallery_activities/24456/gallery_items" class="formtastic gallery_item" enctype="multipart/form-data" id="new_gallery_item" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="A5lmLTkPubF7RXTrMN7+jNrHUsy0rsfHMW+Rpisjzug=" /></div>

    <fieldset class="inputs"><ol>

        <li class="string optional" id="gallery_item_title_input"><label for="gallery_item_title">Title</label><input id="gallery_item_title" maxlength="255" name="gallery_item[title]" type="text" />
<p class="inline-hints">What is this a photo of?</p></li>

        <li class="string optional" id="gallery_item_description_input"><label for="gallery_item_description">Description</label><input id="gallery_item_description" name="gallery_item[description]" type="text" />
<p class="inline-hints">Enter a short description of this photo</p></li>

        <li class="file optional" id="gallery_item_photo_input"><label for="gallery_item_photo">Upload Photo</label><input id="gallery_item_photo" name="gallery_item[photo]" type="file" />
<p class="inline-hints">Maximum 1024x1024, 2 MB, .JPG format</p></li>

        <li class="numeric required" id="gallery_item_position_input"><label for="gallery_item_position">Position<abbr title="required">*</abbr></label><input id="gallery_item_position" name="gallery_item[position]" type="text" value="100" />
<p class="inline-hints">Lower numbers appear first in your gallery</p></li>
</ol></fieldset>
    <fieldset class="buttons"><ol>
        <button class="big button" name="commit" type="submit" value="commit">Save Changes</button>
        <a href="/projects/276345/gallery_activities/24456/edit" class="big button">Cancel</a>
        <a href="http://support.andromo.com/kb/activities/photo-gallery-activity" class="big button" target="_blank">Help</a>
</ol></fieldset>   
</form>

Now the interesting thing is that if I fill the above form in a real browser with the file uploaded, on submission, it takes me to the url: http://www.andromo.com/projects/276345/gallery_activities/24456/edit. But with selenium , it is taking me to http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items, a link which in a real browser(after being logged in ofcourse) takes me to a "sorry this page does not exist" page. So what is going on here? I tried doing it with HtmlUnit also.(Refer this question I posted today), but its giving me the same result.

Was it helpful?

Solution

Your code looks correct to me. I assume that the file exists on your system. You also provide the absolute path of the file to upload which is also right. To me it looks like, the form is not getting submitted correctly. So I would recommend to use submit(); instead of click();

 WebElement commitButton=driver.findElement(By.name("commit"));
 commitButton.submit();

From your comment, it seems like submit(); works for FirefoxDriver() and doesn't for HtmlUnitDriver. I noticed that your HtmlUnitDriver doesn't have Javascript enabled. From the documentation here, try below

HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);

or

HtmlUnitDriver driver = new HtmlUnitDriver(true);

Also make sure you have the latest Selenium library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top