2

I am trying to automate the some workflow. which includes the login to the website and filling couple of data and then Upload pdf file on drag and drop section and upon the upload click on proceed.

I am using the Perl WWW::Mechanize::Firefox module as it supports javascript.

Tried many methods but no luck.

eg web site https://smallpdf.com/split-pdf

Can you help me with. I am trying to emulate the post request by browser into the perl but don't know how to achieve this.

request header is >>>>>>

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

Trying this module

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

Some how I can able to open the filebrowser using the Click method on xpath but then Dont know how to select file.

please suggest me something alternate ways are appreciated Kindly check the example website it has same file upload option.

file is on my local linux server.

Following is the javascript responsible for upload

  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 Answers0