使Casperjs上传图像上传到文件字段的问题 - 尝试的casperjs fill()和phantomjs uploadfile()[英] Issues getting CasperJS to upload image to file field - tried CasperJS fill() and PhantomJS uploadFile()

问题描述

我正在尝试使用casperj上传图像到网络形式.

我的表格看起来像这样:

<form action="" method="POST" enctype="multipart/form-data" class="form-vertical">
    ...                        
        <legend>Campaign Banner</legend>
        <div class="control-group image-field ">
            <label class="control-label">Now Large</label>
            <div class="controls">
                <div class="file-field"><input id="id_now_large_image" name="now_large_image" type="file"></div>
                <div class="image-preview"></div>
                <div class="clear"></div>
                <span class="help-inline"></span>
            </div>
        </div>
        <div class="control-group image-field ">
            <label class="control-label">Now Medium</label>
            <div class="controls">
                <div class="file-field"><input id="id_now_medium_image" name="now_medium_image" type="file"></div>
                <div class="image-preview"></div>
                <div class="clear"></div>
                <span class="help-inline"></span>
            </div>
        </div>
        <div class="control-group image-field ">
            <label class="control-label">Now Small</label>
            <div class="controls">
                <div class="file-field"><input id="id_thumbnail_image" name="thumbnail_image" type="file"></div>
                <div class="image-preview now-small"></div>
                <div class="clear"></div>
                <span class="help-inline"></span>
            </div>
        </div>

提交按钮看起来像这样:

<div class="form-actions">
    <button type="submit" class="btn btn-small ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false"><span class="ui-button-text">Save changes</span></button>
</div>

我在这里放了一个完整的html: https://gist.github.com/victorhooi/8277035

首先,我尝试使用Casperjs的 fill 方法:

    this.fill('form.form-vertical', {
        'now_large_image': '/Users/victor/Dropbox/CMT Test Resources/Test Banners/Default Banners/now_large.jpg',
    }, false);
    this.click(x('//*[@id="content-wrapper"]//button/span'));

我也做了this.capture(),我看到文件字段已填充:

在此处输入图像说明

我没有使用填充方法的内置提交,但是我自己单击"提交"按钮:

this.click(x('//*[@ID =" content-wrapper"]//button/span'));

>

我再做一次捕获,我可以看到该表格已提交:

在此处输入图像说明

但是,该图像似乎根本没有上传.

我还尝试使用phantomjs的 uploadfile(uploadfile) :

this.page.uploadFile('input[name=now_large_image]', '/Users/victor/Dropbox/CMT Test Resources/Test Banners/Default Banners/now_large.jpg');

,然后单击"提交"按钮.

同一问题 - 表单字段被填写 - 但是,图像本身似乎并未提交.

关于我如何在此处正确上传图像的任何想法?

推荐答案

由于您正在成功填补和提交,因此请尝试等待单击.

命令
casper.then(function() {
    //+++ form fill here

    //waits 1 sec
    this.wait(1000, function() {
        this.click(x('//*[@id="content-wrapper"]//button/span'));
    });
});

其他推荐答案

尝试这个.

casper.thenOpen('about:blank', function(){
    this.evaluate(function(){
        var action = 'upload.php'
        var html = '<form action="'+action+'" method="post" enctype="multipart/form-data">'
        html += '<input type="file" name="files[]" multiple="multiple">'
        html += '<button type="submit">Submit</button>'
        html += '</form>'
        document.write(html)
    })
    this.fill('form',{
        'files[]': 'file.txt'
    }, true)
    this.waitFor(function(){
        var uri = casper.evaluate(function(){
            return document.documentURI
        })
        if ( 'about:blank' === uri ){
            return false
        }
        return true
    })
})

本文地址:https://www.itbaoku.cn/post/1739994.html