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

Codeceptjs调用type函数触发Webdriver 404未知命令错误求助

问题:CodeceptJS中使用type函数触发Webdriver 404错误,fillField无法启用页面按钮

问题描述

使用CodeceptJS的type函数时,每次都会触发Webdriver 404错误,错误信息如下:

2022-07-31T14:09:08.666Z ERROR webdriver: Request failed with status 404 due to unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource

尝试改用fillField函数填充字段后,页面上的“Continuar”按钮无法启用。

测试代码

const { I } = inject();

module.exports = {

  tabs:{
    CPFouEmail: "#tabCpf"
  },

  campos:{
    cpf: "#cpf",
    senha: "#senha"
  },

  botao:{
    continuar: "//android.widget.Button[@text='Continuar']"
  },

  fazerLogin(CPF, senha){
    I.waitForElement(this.tabs.CPFouEmail,5)
    I.tap(this.tabs.CPFouEmail)

    I.waitForElement(this.campos.cpf,5)
    I.tap(this.campos.cpf)
    //I.fillField(this.campos.cpf, CPF)
    I.type(CPF)
    
    I.waitForElement(this.botao.continuar,5)
    I.tap(this.botao.continuar)

    I.waitForElement(this.campos.senha,5)
    I.tap(this.campos.senha)
    //I.fillField(this.campos.senha, senha)
    I.type(senha)

    I.waitForElement(this.botao.continuar,5)
    I.tap(this.botao.continuar)
  }
}

Verbose模式错误日志

[1] Error | unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    [1] Error | unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    [1] Error | unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    [1] Error | unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    [1] Error | unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    [1] Error | unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    [1] Error | unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    [1] Error | unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    [1] Error | unknown command: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
    [1] <teardown> Stopping recording promises
 » <screenshotOnFail> Test failed, try to save a screenshot
 » Screenshot has been saved to C:\Users\felip\OneDrive\Documentos\AppVivo\output\Login_com_sucesso.failed.png
  × FAILED in 5943ms

    [2]  Starting recording promises

-- FAILURES:

  1) Login
       Login com sucesso:
     The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
      at getErrorFromResponseBody (node_modules\webdriver\build\utils.js:198:12)
      at NodeJSRequest._request (node_modules\webdriver\build\request\index.js:166:60)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async Browser.wrapCommandFn (node_modules\@wdio\utils\build\shim.js:137:29)
      at async Browser.wrapCommandFn (node_modules\@wdio\utils\build\shim.js:137:29)
      at async Appium.type (node_modules\codeceptjs\lib\helper\WebDriver.js:1895:5)

解决方案

1. 修复type函数的404错误

当前调用I.type(CPF)时未指定目标元素,导致Webdriver找不到对应命令资源。CodeceptJS的type函数需要明确指定元素定位器,正确写法如下:

I.type(CPF, this.campos.cpf);

或者在点击元素后,通过带元素参数的type方法,确保命令发送到正确目标元素上。

2. 解决fillField无法启用按钮的问题

页面按钮未启用通常是因为前端需要监听真实键盘输入事件(如input、keyup),而fillField是直接设置字段值,不会触发这些事件。可尝试以下方案:

  • 在fillField后手动触发输入事件:
    I.fillField(this.campos.cpf, CPF);
    I.executeScript(() => {
      const input = document.querySelector('#cpf');
      input.dispatchEvent(new Event('input', { bubbles: true }));
      input.dispatchEvent(new Event('keyup', { bubbles: true }));
    });
    
  • 使用带元素定位器的type函数模拟真实键盘输入,自动触发所需前端事件,从而启用按钮。

3. 额外建议

  • 确认CodeceptJS、WebdriverIO和Appium版本兼容性,版本不匹配也可能引发这类未知命令错误。
  • 移动端测试时,确保元素定位器(如#cpf)能被Appium正确识别,必要时改用原生定位策略(如id、accessibilityId)。

内容的提问来源于stack exchange,提问作者Felipe Rossi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 17:55:47