【问题标题】:Unable to upload file using Selenium web driver无法使用 Selenium Web 驱动程序上传文件
【发布时间】:2014-02-13 14:59:11
【问题描述】:

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

        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>

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

【问题讨论】:

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


    【解决方案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 库。

    【讨论】:

    • 感谢您的回答。但效果一样!我想知道 java 中是否存在通过浏览器自动化将文件上传到表单的任何方式,两个最大的玩家 HtmlUnit 和 Selenium webDriver 失败了!
    • 是的!你可以把它作为答案。你为我节省了一天!我已经对此表示赞同。但令人惊讶的是,为什么 htmlunit 驱动程序不起作用?
    • 我更新了答案。你可以检查并恢复吗?
    • 今天会看一下并恢复。但我通过将其更改为 FirefoxDriver 来做到这一点。尽管我对您的答案的实验肯定会帮助某人,因此会尝试。再次感谢!
    • 刚刚接受了。请回答 FirefoxDriver 可以代替 HtmlUnitDriver。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2015-09-17
    • 2012-08-17
    • 2023-04-04
    • 2020-10-10
    • 2020-01-19
    • 2019-01-06
    相关资源
    最近更新 更多