Failed to load file using Selenium web driver.

I am trying to upload a file using selenium web driver according to 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 doesn't load 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 html form element with which I am trying to upload a 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 what is interesting is that if I fill out the above form in a real browser with the file uploaded, according to the submission, it takes me to the URL: http://www.andromo.com/projects/276345/gallery_activities/24456/edit . But with selenium, it leads me to http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items , a link that in a real browser (after entering the course) takes me to the page "sorry, this page does not exist ". and so what's going on here? I have also tried doing this with HtmlUnit (see this question I posted today), but it gives me the same result.

0


source to share


1 answer


Your code looks correct to me. I am assuming the file exists on your system. You also provide the absolute file path to download, which is also right. To me it looks like the form is not being submitted correctly. So I would recommend using submit();

insteadclick();

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

      

From your comment, it seems to submit();

work for FirefoxDriver()

and not for HtmlUnitDriver

. I noticed yours HtmlUnitDriver

doesn't support Javascript. 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.

+1


source







All Articles