使用selenium和python与jQuery事件交互

2024-05-05 02:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个网站,其中包含一个标签,如下所示:

<table><tr style=""><td style="padding: 3px;" colspan="2"><div style="position: relative; padding: 10px; border: 1px solid black; cursor: pointer;"><div style="position: absolute; background-color: rgba(50, 100, 50, 1); left: 0; top: 0; bottom: 0; z-index: 0; width: 0%"></div><label style="color: white; white-space: nowrap; position: relative; z-index: 1; cursor: pointer;">ADD ATTACHMENT +</label></div></td></tr></table>

然后,此标签上有一个jQuery事件,如下所示:

function attachClickHandler(widget) {
    widget.click(function (event) {
        var fileSelect = $("<input name='doc' type='file'></input>");

        fileSelect.change(function (event) {
            if (!event.target.files.length) {
                return;
            }
            refreshAttachments(false);
            var reader = new FileReader();
            reader.onload = function (readerEvt) {
                var fileDataUri = readerEvt.target.result;
                var fileData = fileDataUri.substr(fileDataUri.indexOf(',') + 1);
                if (fileData.length > 1024 * 1024 * 87) {
                    showDialog("Upload size is limited to 50MB.");
                    return;
                }

                $.post('prepareAttachment', {
                    SYNCHRONIZER_TOKEN: syncToken,
                    SYNCHRONIZER_URI: syncURI,
                    fileName: fileName,
                    fileSize: fileData.length
                }, function (data) {
                    if (!(data instanceof Object)) {
                        showSessionExpired();
                        return;
                    }

                    function uploadPart(partIndex) {
                        $.post('uploadAttachmentPart', {
                            SYNCHRONIZER_TOKEN: syncToken,
                            SYNCHRONIZER_URI: syncURI,
                            attachmentId: attachmentId,
                            partIndex: partIndex,
                            partData: fileData.substr(partIndex * partSize, partSize)
                        }, function (data) {
                            if (!(data instanceof Object)) {
                                showSessionExpired();
                                return;
                            }
                            if (progressDiv) progressDiv.css("width", (100.0 * (partIndex + 2) / (numParts + 1)) + "%");
                            console.log(data);
                            if (partIndex + 1 === numParts) {
                                if (!data.id) {
                                    showDialog("Uploading file failed.");
                                } else {
                                    attachments.push({
                                        id: data.id,
                                        fileName: fileName
                                    });
                                }
                                refreshAttachments(true);
                            } else {
                                uploadPart(partIndex + 1);
                            }
                        }).fail(function () {
                            showDialog("Uploading file failed.");
                            refreshAttachments(true);
                        });
                    }

                    console.log(data);
                    var partSize = data.partSize;
                    var numParts = Math.ceil(fileData.length / partSize);
                    var attachmentId = data.attachmentId;
                    console.log(progressDiv);
                    if (progressDiv) progressDiv.css("width", (100.0 / (numParts + 1)) + "%");
                    uploadPart(0);
                }).fail(function () {
                    showDialog("Uploading file failed.");
                    refreshAttachments(true);
                });
            };
            var file = event.target.files[0];
            var fileName = file.name;
            var extension = file.name.substring(file.name.lastIndexOf(".") + 1);
            if (!extension) {
                showDialog("Failed to upload. Filename must have an extension.");
                return;
            }
            reader.readAsDataURL(file);
        });
        fileSelect.click();
    });
}

单击“添加附件”按钮时,它将调用var fileSelect=$(“”);要打开一个文件选择对话框,我想使用selenium将_键发送到输入,但是由于它是由jQuery生成的,我无法打开,有人知道我如何在windows对话框打开后与它交互,或者通过类似执行_脚本的方式将文件路径传递到站点吗

谢谢