温馨提示:本文翻译自stackoverflow.com,查看原文请点击:html - Unable to upload a file to the "browse" button using sendKeys() in Selenium Java

html - 无法使用Selenium Java中的sendKeys()将文件上传到“浏览”按钮

发布于 2020-04-07 10:09:03

我正在尝试自动执行上传文件功能。我尝试使用不同的xpaths进行获取,input element但是似乎我做的事情可能不正确。我的输入元素没有ID

<input name="qqfile" title="file input" style="margin: 0px; padding: 0px; top: 0px; height: 100%; right: 0px; font-family: Arial; font-size: 3500px; position: absolute; cursor: pointer; opacity: 0;" type="file" qq-button-id="7ca76e6f-e8ac-49f4-ab1e-c143e4af60d8">

并且在同一个HTML中还有另一个具有相同名称的输入元素。

<input name="qqfile" title="file input" style="margin: 0px; padding: 0px; top: 0px; height: 100%; right: 0px; font-family: Arial; font-size: 3500px; position: absolute; cursor: pointer; opacity: 0;" type="file" multiple="" qq-button-id="b5ebbdcb-6f33-4f10-a569-5dba94f54d0e">

因此,当我使用时,By.xpath("//input[contains(@name,'qqfile')]我得到一个Element not visible exception 我认为这是由于上下文中存在两个具有相同名称的元素。如何唯一标识每个元素?

其他HTML代码:

<div tabindex="15" id="annUploadDoc"><div class="qq-uploader-selector qq-uploader" qq-drop-area-text="Drop files here">
            <div class="qq-total-progress-bar-container-selector qq-total-progress-bar-container qq-hide">
                <div class="qq-total-progress-bar-selector qq-progress-bar qq-total-progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
            </div>
            <div class="qq-upload-drop-area-selector qq-upload-drop-area" style="display: none;" qq-hide-dropzone="">
                <span>Drop files here to upload</span>
            </div>
            <div class="qq-upload-button-selector qq-upload-button" style="overflow: hidden; position: relative; direction: ltr;">
                <div class="btn upload btn-style-3">
                    <div>Browse</div>
                </div>
            <input name="qqfile" title="file input" style="margin: 0px; padding: 0px; top: 0px; height: 100%; right: 0px; font-family: Arial; font-size: 3500px; position: absolute; cursor: pointer; opacity: 0;" type="file" qq-button-id="fd389870-bdb2-480e-8138-8f2ff762de22"></div>
            <span class="qq-drop-processing-selector qq-drop-processing qq-hide">
                <span>Processing dropped files...</span>
                <span class="qq-drop-processing-spinner-selector qq-drop-processing-spinner"></span>
            </span>
            <ul class="qq-upload-list-selector qq-upload-list" aria-live="polite" aria-relevant="additions removals"></ul>

            <dialog class="qq-alert-dialog-selector">
                <div class="qq-dialog-message-selector"></div>
                <div class="qq-dialog-buttons">
                    <a class="qq-cancel-button-selector" href="javascript:void(0)">Close</a>
                </div>
            </dialog>

            <dialog class="qq-confirm-dialog-selector">
                <div class="qq-dialog-message-selector"></div>
                <div class="qq-dialog-buttons">
                    <a class="qq-cancel-button-selector" href="javascript:void(0)">No</a>
                    <a class="qq-ok-button-selector" href="javascript:void(0)">Yes</a>
                </div>
            </dialog>

            <dialog class="qq-prompt-dialog-selector">
                <div class="qq-dialog-message-selector"></div>
                <input type="text">
                <div class="qq-dialog-buttons">
                    <a class="qq-cancel-button-selector" href="javascript:void(0)">Cancel</a>
                    <a class="qq-ok-button-selector" href="javascript:void(0)">Ok</a>
                </div>
            </dialog>
        </div></div>

查看更多

提问者
PratikshaB
被浏览
55
PratikshaB 2020-01-31 19:15

我找到了解决我问题的方法。我正在使用的是IEDriver。显然,IEDriver以某种方式呈现了网页元素。切换到ChromeDriver后,使用SendKeys()上传的文件效果很好。