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

如何解决WebDriverIO测试中页面模块导入错误导致的元素调用异常问题

Fixing WebDriverIO Element Access Issue in Page Object Module

Let's break down what's going wrong here and fix it—your issue isn't actually about module importing, it's about how you're accessing elements in your page object.

The Root Cause

Looking at your main.pageHW.js, topSignUpBtn is defined as a getter property (using the get keyword), not a method. When you call it with parentheses like mainPage.topSignUpBtn() in your test, you're trying to invoke the Element object returned by the getter as if it were a function. That's why you're getting unexpected output (like a UUID) instead of the element you need.

Solution 1: Use the getter correctly (no parentheses)

Modify your test code to access the getter property without calling it as a function:

describe('final homeTask', () => {
  it('firstassigment', async () => {
    let mainPage = require('../page/main.pageHW.js')
    await browser.url('https://github.com/')
    // Remove the parentheses after topSignUpBtn
    let signUpButton = await mainPage.topSignUpBtn
    await signUpButton.click()
  })
})

Solution 2: Convert the getter to a method (if you prefer parentheses)

If you want to keep using parentheses in your test code, adjust the page object to define topSignUpBtn as a regular method instead of a getter:

class MainPage {
  // Remove the `get` keyword and add parentheses to make it a method
  topSignUpBtn() {
    return $('a.btn-mktg:nth-child(1)')
  }
}
module.exports = new MainPage()

Now your original test code with mainPage.topSignUpBtn() will work as expected.

Why Direct Element Access Works

When you directly use await $('a.btn-mktg:nth-child(1)') in your test, you're correctly fetching the element without any incorrect function calls. The confusion came from mixing up getter properties and methods in your page object.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 21:52:36