zaf*_*005 4 javascript python wordpress selenium file-upload
我试图使用Selenium Webdriver (Python). 不幸的是,我无法上传帖子内容中的文件。我已经搜索了解决方案,但大多数使用的解决方案send_keys不适用于 WP 插入介质(见下图)。在插入媒体中,有两个选项:
我需要一个适用于跨平台(Windows、Linux 等)的解决方案。\n我想可能有一种使用 jQuery、JS 或其他东西的方法。我对JS不太熟悉,所以不太了解JS的解决方案。
\n\nPS 我正在使用 Python 工作,因此 Python 代码会很有帮助。
\n\n\n\n以下是媒体插入框架的来源,以防您需要它们:
\n\n<div class="media-frame-content">\n <div class="uploader-inline">\n\n <div class="uploader-inline-content no-upload-message">\n\n <div class="upload-ui">\n <h2 class="upload-instructions drop-instructions">Drop files anywhere to upload</h2>\n <p class="upload-instructions drop-instructions">or</p>\n <a href="#" class="browser button button-hero" style="display: inline; position: relative; z-index: 1;" id="__wp-uploader-id-1">Select Files</a>\n </div>\n\n <div class="upload-inline-status">\n <div class="media-uploader-status" style="display: none;">\n <h2>Uploading</h2>\n <button type="button" class="button-link upload-dismiss-errors"><span class="screen-reader-text">Dismiss Errors</span>\n </button>\n\n <div class="media-progress-bar">\n <div></div>\n </div>\n <div class="upload-details">\n <span class="upload-count">\n <span class="upload-index"></span> / <span class="upload-total"></span>\n </span>\n <span class="upload-detail-separator">\xe2\x80\x93</span>\n <span class="upload-filename"></span>\n </div>\n <div class="upload-errors"></div>\n </div>\n </div>\n\n <div class="post-upload-ui">\n\n <p class="max-upload-size">Maximum upload file size: 32 MB.</p>\n\n </div>\n </div>\n </div>\n</div>\nRun Code Online (Sandbox Code Playgroud)\n
最后,我得到了 WP 添加媒体文件上传的解决方案。实际上,我发现当单击“选择文件”按钮打开文件选择窗口时,它会生成一个动态输入字段。动态意味着,输入字段的 ID 每次都是唯一的。幸运的是,输入字段 ID 的第一部分保持不变。例如,ID 类似于html5_1bc7564i41pq5f7m1voce561a0e5。请参阅下面的 HTML:
<input id="html5_1bc7564i41pq5f7m1voce561a0e5" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;" multiple="" accept="" type="file"/>
Run Code Online (Sandbox Code Playgroud)
所以,我所做的是,使用 ID 的第一部分 (html5_) 创建一个 XPATH,并简单地使用它send_keys,它工作得很好。我的 XPATH 是这样的:
input_file = "//input[starts-with(@id,'html5_')]"
硒代码是:
driver.find_element_by_xpath(input_file).send_keys(file_path)
这里file_path是我需要上传的文件的位置。
我首先错过了输入字段,因为它不可见并且没有意识到它与文件上传相关联。于是我用selenium IDE记录了文件上传步骤,并找到了动态输入ID。
感谢大家的建议和指导。
| 归档时间: |
|
| 查看次数: |
3394 次 |
| 最近记录: |