請幫助我,如何上傳文件。單擊時附加文件輸入,但單擊時會打開OS本機窗口


2

無法上傳文件。DOM元素:

<button type="button" id="button-upload" data-loading-text="Loading..." class="btn btn-primary"><i class="fa fa-upload"></i> Upload</button>

$('#button-upload').on('click', function() {
$('#form-upload').remove();

$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

$('#form-upload input[name=\'file\']').trigger('click');

if (typeof timer != 'undefined') {
    clearInterval(timer);
}

timer = setInterval(function() {
    if ($('#form-upload input[name=\'file\']').val() != '') {
        clearInterval(timer);

        // Reset everything
        $('.alert-dismissible').remove();
        $('#progress-bar').css('width', '0%');
        $('#progress-bar').removeClass('progress-bar-danger progress-bar-success');
        $('#progress-text').html('');

        $.ajax({
            url: 'index.php?route=marketplace/installer/upload&user_token=user_token_here',
            type: 'post',
            dataType: 'json',
            data: new FormData($('#form-upload')[0]),
            cache: false,
            contentType: false,
            processData: false,
            beforeSend: function() {
                $('#button-upload').button('loading');
            },
            complete: function() {
                $('#button-upload').button('reset');
            },
            success: function(json) {
                if (json['error']) {
                    $('#progress-bar').addClass('progress-bar-danger');
                    $('#progress-text').html('<div class="text-danger">' + json['error'] + '</div>');
                }

                if (json['text']) {
                    $('#progress-bar').css('width', '20%');
                    $('#progress-text').html(json['text']);
                }

                if (json['next']) {
                    next(json['next'], 1);
                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    }
}, 500);
});

我認為輸入是在單擊按鈕後出現的。但是它會打開OS對話框。我想發送沒有操作系統對話框的文件。現在我正在使用python。但是重要的是使用硒油或其他簇進行並行測試。

請幫助我了解操作方法。

0

In one of my projects we had to upload a .csv file. In order to achieve this we were storing the test files with in the solution and then uploading the files as needed. I used C# to implement this, but i am assuming you can try a similar approach in python.

// Get the path of the folder where you stored the files

public string GetPath()

{

 var rootPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

 var path = String.Concat(rootPath, @"\TestData\"); //I am storing my files in this folder

 return path;

}

//Construct the relative path

var relpath =(Path + "nameoffile.csv");

//Upload the file

public void Upload(string path)

{

    if (File.Exists(relpath)

            { 

                UploadButton.SendKeys(relpath);
            }
            else
            {
                throw new Exception(String.Concat("File could not found "));
            }
}

Hope the above gives you an idea


1

you can create the input tag without triggering the click event by using javascript executor:

body=driver.find_element_by_tag_name('body');

driver.execute_script("$(arguments[0]).prepend('<form enctype=\"multipart/form-data\" id=\"form-upload\" style=\"display: block;\"><input type=\"file\" name=\"file\" /></form>')", body);

Note:

I just used the same command from the script you are provided in the question. Just note that here we are using $(arguments[0]) and not just arguments[0]; Here, $() is the jquery constructor that requires the element or element locator as the parameter.

Also,

I have set style=\"display: block;\" to make sure the input element is visible in UI for you make sure this script is valid. Once you are satisfied you can replace the block with 'none' as in your question. This time you cannot see the file after upload.

Suggestion:

As the UI has lots of activities like a progress bar, upload status etc it does make sense to use Autoit, silkulix etc to upload the file using upload window and validate all the features like a progress bar, status change etc. it is not recommended to skip validating these things as its an important feature in you UI