无法使用Selenium Web驱动程序上传文件 [英] Unable to upload file using Selenium web driver

查看:100
本文介绍了无法使用Selenium Web驱动程序上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

文件未上传,输出为:

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,我正尝试使用该html上传文件:

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>

现在有趣的是,如果我在真实的浏览器中将上传的文件填写到上面的表格中,则提交后,它会将我带到以下网址: http://www.andromo.com/projects/276345/gallery_activities/24456/edit .但是使用硒,它将带我到 http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items ,该链接在真实浏览器中(当然,在登录后)将我带到抱歉,此页面不存在"页面.那么这是怎么回事?我也尝试过使用HtmlUnit.(请参阅这个问题(我今天发布的问题),但是它给了我相同的结果.

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.

推荐答案

您的代码对我来说是正确的.我认为该文件存在于您的系统上.您还提供了上载文件的绝对路径,这也是正确的.在我看来,表单未正确提交.所以我建议使用submit();代替click();

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

根据您的评论,看来submit();适用于FirefoxDriver()而不适用​​于HtmlUnitDriver.我注意到您的HtmlUnitDriver没有启用Javascript.在文档此处中,尝试以下

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

HtmlUnitDriver driver = new HtmlUnitDriver(true);

还要确保您拥有最新的Selenium库.

Also make sure you have the latest Selenium library.

这篇关于无法使用Selenium Web驱动程序上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆