如何使用WWW :: Mechanize :: Firefox在网页上上传文件

时间:2017-06-18 16:08:03

标签: javascript linux perl perl-module www-mechanize-firefox

我正在尝试自动化某些工作流程。其中包括登录网站并填写几个数据,然后在拖放部分上传pdf文件,点击上传后继续。

我正在使用Perl WWW :: Mechanize :: Firefox模块,因为它支持javascript。

尝试了很多方法,但没有运气。

例如网站https://smallpdf.com/split-pdf

你可以帮我吗?我试图通过浏览器将post请求模拟到perl中,但不知道如何实现这一点。

请求标头是>>>>>>>

Host: XXXXX.XXXXXXX.XXX
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Cache-Control: no-cache
X-Requested-With: XMLHttpRequest
Referer: https://login.XXXXXX.XXXXX/XXXXXXXX
Content-Length: 26712
Content-Type: multipart/form-data; boundary=---------------------------10666735941660241595971092194
Cookie: _pk_id.2.a970=e6850e4973836855.1497637919.11.1497792387.1497791018.; XSRF-TOKEN=eyJpdiI6InplN3NRck9CdDlFem54ekpxbmxUMXc9PSIsInZhbHVlIjoiYU41TXhLS1hYbmtvNUo0ejQ3Tk1EZ2ZMS2FUdGRuYVwvc3Q5R3RwMkpHQklCTkpkdlBFUUJDeVJYUlhQRWkyOGhVK3FwK1hWdHlMZEtSVGFcLzNhaTF2QT09IiwibWFjIjoiYjg5NDE3YjhiMWMwMmI0NTI3NzRmNTM0NmRhNTUxY2VkZTM3YWRmYjlkMmNmY2IwOGRlNzVjYzc3NjU1NTg4YyJ9; laravel_session=eyJpdiI6ImFzdHpodTJlU0toOWZLeUQ3QTExV1E9PSIsInZhbHVlIjoiaDBpZGcwMGxtb3k4a3BYTzJhVjJLOEZOOUNWekJVUjB3YTk4QjhKMUJIWFhhK3BwWTRzd2Nxekp1T3pZRVNnZUhlWjBHZGNUeW1sNDBza0NnN0hTd3c9PSIsIm1hYyI6ImM0Y2ZhOWY1MmY2NGM3YTdiNDFmY2EzZjg3NmY1ODk2MTQyOGZkOWQxODdjNzc0OTU1ZmVkYjE2NTQ3MjU3OTgifQ%3D%3D; _pk_ses.2.a970=*
Connection: keep-alive

尝试此模块

my $mech = WWW::Mechanize::Firefox->new();
$mech->cookies();

我可以使用xpath上的Click方法打开文件浏览器但不知道如何选择文件。

请建议我一些其他的方式被赞赏请检查示例网站它有相同的文件上传选项。

文件在我的本地linux服务器上。

以下是负责上传的javascript

  function sendFax(aRecipients) {
    $('#FaxResponseModal #ajaxResponse').html("");
    $('#FaxResponseModal').modal('show');

    $sFormData = $('form').serialize();

    $ajaxLoader = $('.ajax-modal-loading').detach();

    $.each(aRecipients, function (iKey, sRecipient) {

        $_sFormData = $sFormData + '&to_' + sRecipient + '=' + sRecipient;

        $.post('/fax_Senden', $_sFormData).done(function (oResponse) {

            $oResponse = JSON.parse(oResponse);

            $.each($oResponse.data, function (sKey, sValue) {
                $_oRow = $("<tr></tr>");
                $_oIconCell = $("<td><span class='glyphicon'></span></td>");
                $_oNumberCell = $("<td>" + sKey + "</td>");
                $_oStatusCell = $("<td></td>");

                if (sValue.Code == 100 || sValue.Code == 101) {
                    $($_oIconCell).find('span').addClass('glyphicon-ok text-success');
                    $($_oRow).addClass('active');
                } else {
                    $($_oIconCell).find('span').addClass('glyphicon-flash');
                    $($_oRow).addClass('danger');
                }

                $($_oStatusCell).text(sValue.Message);

                $_oRow.append($_oIconCell).append($_oNumberCell).append($_oStatusCell);

                $('#FaxResponseModal #ajaxResponse').prepend($_oRow);
            });

        });

    });

    $ajaxLoader.appendTo("body");
}





$(document).ready(function () {
    $(".dropzone").dropzone({
        paramName: "file",
        url: "/fax_Senden/upload",
        autoProcessQueue: true,
        uploadMultiple: false,
        acceptedFiles: "application/pdf",
        maxFiles: 1,
        dictDefaultMessage: "Legen Sie hier die PDF Datei ab.",
        previewTemplate: '<div class="dz-preview dz-file-preview">' +
        '<div class="dz-image">' +
        '<img data-dz-thumbnail />' +
        '</div>' +
        '<div class="dz-details">' +
        '<div class="dz-filename"><span data-dz-name></span></div>' +
        '<div class="dz-size" data-dz-size></div>' +
        '</div>' +
        '<div class="dz-progress"><span class="dz-upload" data-dz-uploadprogress></span></div>' +
        '<div class="dz-success-mark"><span>✔</span></div>' +
        '<div class="dz-error-mark"><span>✘</span></div>' +
        '<div class="dz-error-message"><span data-dz-errormessage></span></div>' +
        '</div>',
        init: function () {
            this.on("addedfile", function () {
                if (this.files[1] != null) {
                    this.removeFile(this.files[0]);
                }
            });
        },

        sending: function (file, xhr, formData) {
            formData.append('_token', $('input[name="_token"]').val());
        },

        success: function (file, response) {
            $('button.segment_fileupload').removeClass('disabled');
            $('.uploadError').addClass('hidden');
        },

        error: function (file, errorMsg, response) {
            $('button.segment_fileupload').addClass('disabled');
            $('.uploadError').removeClass('hidden');
        }
    });


    $('input[type=radio][name=pdfcreate]').change(function () {
        $('.segment_freitext').hide();
        $('.segment_fileupload').hide();

        $('.segment_' + $(this).val()).show();
    });

    $('button[name="preview"].segment_freitext').on('click', function () {

        $('#editor').val(CKEDITOR.instances.editor.getData());

        $.post('/fax_Senden', $('form').serialize() + '&preview=true', function (data) {

            $('#previewPDFModal .modal-dialog').css('width', '75vw');
            $('#previewPDFModal .modal-body').html(
                '<object data="data:application/pdf;base64,' + data + '" type="application/pdf" width="100%" height="100%" style="min-height: 55vh">' +
                '<iframe src="data:application/pdf;base64,' + data + '" width="100%" height="100%" style="border: none;">' +
                'This browser does not support PDFs. Please download the PDF to view it: <a href="data:application/pdf;base64,' + data + '">Download PDF</a>' +
                '</iframe>' +
                '</object>'
            );

            $('#previewPDFModal').modal('show');

        });

    });

    $('button[name="preview"].segment_fileupload').on('click', function () {

        if ( $(this).hasClass("disabled") ) {
            return;
        }

        $('#editor').attr('disabled', 'disabled');
        formData = $('form').serialize();
        $('#editor').removeAttr('disabled');

        $('.ajax-modal-loading').show();
        source = new EventSource('/fax_Senden/preview?' + formData);

        //a message is received
        source.addEventListener('message', function (e) {
            var data = JSON.parse(e.data);

            if (e.data.search('FILE') != -1) {
                source.close();

                $('#previewPDFModal .modal-dialog').css('width', '75vw');
                $('#previewPDFModal .modal-body').html(
                    '<object data="data:application/pdf;base64,' + data.message + '" type="application/pdf" width="100%" height="100%" style="min-height: 55vh">' +
                    '<iframe src="data:application/pdf;base64,' + data.message + '" width="100%" height="100%" style="border: none;">' +
                    'This browser does not support PDFs. Please download the PDF to view it: <a href="data:application/pdf;base64,' + data.message + '">Download PDF</a>' +
                    '</iframe>' +
                    '</object>'
                );

                $('.ajax-modal-loading').html("");
                $('#previewPDFModal').modal('show');
                $('.ajax-modal-loading').hide();
            }

            if (data.message == 'progress') {

                $('.ajax-modal-loading').html(
                    "<p class='generateMsg'>Bearbeite Seite " + data.progress + "</p>"
                );
            }
        });

        source.addEventListener('error', function (e) {
            //kill the object ?
            source.close();
        });

    });

0 个答案:

没有答案