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

无input[type='file']标签时如何在Cypress中实现文件上传

Cypress实现非input触发的文件上传方案

问题场景

需在Cypress中完成文件上传操作,但触发上传的按钮并非<input>标签,也不具备type="file"属性。

尝试过的方案及问题

方案一

cy.contains('div#dropdown-grouped', "Bon d'intervention").siblings('div.d-none').find('input[type="file"]') 
    .selectFile('cypress/fixtures/bon.pdf', {force: true})

执行后返回422状态码,上传失败。

方案二

const filePath = 'My reportis.jpg'
cy.contains('#dropdown-group-1', "Bon d'intervention").attachFile(filepath)

执行后无任何响应,未触发上传流程。

疑问

是否可以通过修改按钮标签为<input>并添加type="file"属性来实现上传?如果可行,如何在Cypress中修改DOM标签?

最终可行方案

cy.contains('#dropdown-group-1', "Bon d'intervention").click()
cy.get('input[type="file"]').selectFile('cypress/fixtures/bon.pdf', {force: true})

内容的提问来源于stack exchange,提问作者Andrei Nico

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 08:36:19