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

Puppeteer 23.2.1调用page.setInputFiles报错,VS可识别该方法求助

解决Puppeteer 23.2.1中page.setInputFiles报错的问题

核心原因

Puppeteer从v19版本开始弃用page.setInputFiles,v21版本后彻底移除了这个方法,替换为page.setFileInputFiles。你使用的23.2.1版本已经没有这个旧API了,VS能识别是因为IDE的类型定义缓存或者依赖的类型文件未同步更新。

解决线索

  • 替换API方法
    直接把代码里的page.setInputFiles改为page.setFileInputFiles,修改后的代码片段:

    // 替换这行
    await page.setFileInputFiles('input[type="file"]', '/path/to/your/file.jpg');
    
  • 同步类型定义与运行版本
    检查项目中Puppeteer的类型文件是否和23.2.1版本匹配:

    1. 删除node_modules和package-lock.json文件
    2. 重新执行npm install puppeteer@23.2.1安装对应版本
    3. 重启VS Code,清理IDE缓存
  • 改用元素句柄调用
    先获取元素句柄,再调用元素自身的setInputFiles方法(该方法在元素句柄上仍保留):

    await page.waitForSelector('input[type="file"]');
    const inputHandle = await page.$('input[type="file"]');
    await inputHandle.setInputFiles('/path/to/your/file.jpg');
    

修改后的完整代码

const puppeteer = require('puppeteer');

async function testSetInputFiles() {
    const browser = await puppeteer.launch({ 
        headless: false, 
        args: ['--no-sandbox', '--disable-setuid-sandbox'] 
    });
    
    const page = await browser.newPage();

    try {
        await page.goto('https://www.w3schools.com/howto/howto_html_file_upload_button.asp');

        // Wait for the file input element and then set the file
        await page.waitForSelector('input[type="file"]');
        await page.setFileInputFiles('input[type="file"]', '/path/to/your/file.jpg');

        console.log('File uploaded successfully.');
    } catch (error) {
        console.error('Error:', error);
    } finally {
        await browser.close();
    }
}

testSetInputFiles();

内容的提问来源于stack exchange,提问作者Jean François Manatane

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 23:57:38