You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Playwright(JS)在iframe内上传图片?报错求助

解决方案

报错Node is not an HTMLInputElement的核心原因是你定位的元素不是<input type="file">类型的文件上传控件——你当前选中的是//*[@id="edit-upload"]/div这个div容器,而setInputFiles方法只能作用于原生的文件输入框。

解决步骤:

  • 找到iframe内真实的<input type="file">元素,这类元素通常隐藏在上传按钮的下层,或者是上传区域关联的原生输入控件。
  • 修改定位器,直接指向该input元素,再调用setInputFiles。

修改后的代码示例:

const mFrame = page.frameLocator('iframe[name="entity_browser_iframe_eb_banner_slides"]');

// 点击Upload按钮
await mFrame.locator('a:has-text("Upload")').click();

// 定位到type为file的input元素(若id明确可直接用id定位,比如#edit-upload-file)
const fileInput = mFrame.locator('input[type="file"]');

// 可选:等待元素加载完成,避免因DOM未渲染导致定位失败
await fileInput.waitFor();

// 执行文件上传
await fileInput.setInputFiles(filepath);

如果上述通用定位器input[type="file"]无法找到目标元素,你可以通过浏览器开发者工具查看上传区域的DOM结构,找到对应file input的具体选择器(比如父容器id结合input类型:#edit-upload input[type="file"])。

内容的提问来源于stack exchange,提问作者sadhish jay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 22:01:01