无法使用Selenium Web驱动程序上传文件

时间:2014-02-13 14:59:12

标签: java file-upload selenium selenium-webdriver htmlunit-driver

我正在尝试使用selenium web驱动程序通过以下代码上传文件:

        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();

文件未上传,输出为:

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

这是我试图上传文件的表单元素的html:

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

现在有趣的是,如果我在真实的浏览器中填写上传的文件,在提交时,它会将我带到网址:http://www.andromo.com/projects/276345/gallery_activities/24456/edit。但是对于selenium,它将我带到http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items,这是一个在真实浏览器中的链接(在登录之后)将我带到“抱歉此页面不存在”页面。那么这里发生了什么?我也尝试过使用HtmlUnit。(请参阅我今天发布的this问题),但它给了我相同的结果。

1 个答案:

答案 0 :(得分:1)

您的代码对我来说是正确的。我假设您的系统上存在该文件。您还提供了要上载的文件的绝对路径,这也是正确的。对我而言,表单未正确提交。因此,我建议您使用submit();代替click();

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

从您的评论中,似乎submit();适用于FirefoxDriver(),而HtmlUnitDriver则不适用。我注意到您的HtmlUnitDriver没有启用Javascript。在文档here中,请尝试以下

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

HtmlUnitDriver driver = new HtmlUnitDriver(true);

另外,请确保您拥有最新的Selenium库。

相关问题