我试图突出一个元素.请帮我查一下这个代码.(我是nodejs的初学者)

    class Utilities {
    
        async highlight(element){
               await browser.execute((userelement)=>{
                    userelement.style.backgroundColor="red";
                 },element);
                }
    
    }

module.exports = new Utilities();

Stepdef:

Utilities.highlight(LoginPage.getusername);

页面对象:

class LoginPage{
    get getusername(){
        return $('#username');
    }
}

错误:

TypeError: elem[prop] is not a function
    at \node_modules\@wdio\utils\build\shim.js:253:38
    at processTicksAndRejections (node:internal/process/task_queues:96:5)..

详细错误日志(log):

[0-0] 2022-07-18T12:50:08.380Z INFO webdriver: DATA { [0-0] script: 'return ((userelement)=>{\r\n' + [0-0] '
userelement.style.backgroundColor="red";\r\n' + [0-0] '
}).apply(null, arguments)', [0-0] args: [ Promise { } ] [0-0] } [0-0] 2022-07-18T12:50:08.398Z INFO webdriver: RESULT { [0-0] 'element-6066-11e4-a52e-4f735466cecf': 'be8745a8-6499-482b-af98-8dc191b88452' [0-0] } [0-0] Error in "0: When I login with nithildani and asdas123" TypeError: elem[prop] is not a function at C:\Users\nitarula\Documents\Sample2\node_modules@wdio\utils\build\shim.js:253:38 at processTicksAndRejections (node:internal/process/task_queues:96:5) [0-0] 2022-07-18T12:50:08.417Z INFO webdriver: COMMAND deleteSession() [0-0] 2022-07-18T12:50:08.417Z INFO webdriver: [DELETE] http://localhost:9515/session/36ae82293a3b853a2965d478c51f4235 [0-0] 2022-07-18T12:50:08.425Z INFO webdriver: RESULT { [0-0]
'element-6066-11e4-a52e-4f735466cecf': '66da9a07-388f-445c-90ef-9c7a12c29856' [0-0] } [0-0] 2022-07-18T12:50:08.429Z INFO webdriver: COMMAND elementClear("66da9a07-388f-445c-90ef-9c7a12c29856") [0-0] 2022-07-18T12:50:08.429Z INFO webdriver: [POST] http://localhost:9515/session/36ae82293a3b853a2965d478c51f4235/element/66da9a07-388f-445c-90ef-9c7a12c29856/clear [0-0] 2022-07-18T12:50:08.438Z WARN webdriver: Request failed with status 500 due to javascript error: Cannot set properties of undefined (setting 'backgroundColor') [0-0] (Session info: chrome=103.0.5060.114) [0-0] 2022-07-18T12:50:08.439Z INFO webdriver: Retrying 1/3 [0-0] 2022-07-18T12:50:08.439Z INFO webdriver: [POST] http://localhost:9515/session/36ae82293a3b853a2965d478c51f4235/execute/sync [0-0] 2022-07-18T12:50:08.440Z INFO webdriver: DATA { [0-0] script: 'return ((userelement)=>{\r\n' + [0-0] '
userelement.style.backgroundColor="red";\r\n' + [0-0] '
}).apply(null, arguments)', [0-0] args: [ Promise { [Element] } ] [0-0] } [0-0] C:\Users\nitarula\Documents\Sample2\node_modules@wdio\utils\build\shim.js:253 [0-0] return elemprop; [0-0]
^ [0-0] [0-0] TypeError: elem[prop] is not a function [0-0] at C:\Users\nitarula\Documents\Sample2\node_modules@wdio\utils\build\shim.js:253:38 [0-0] at processTicksAndRejections (node:internal/process/task_queues:96:5) { [0-0]
[Symbol(originalCallSite)]: [ CallSite {}, CallSite {} ], [0-0]
[Symbol(mutatedCallSite)]: [ CallSite {}, CallSite {} ] [0-0] } [0-0] FAILED in chrome - C:\Users\nitarula\Documents\Sample2\features\login.feature 2022-07-18T12:50:08.499Z INFO @wdio/cli:launcher: Run onWorkerEnd hook 2022-07-18T12:50:08.502Z INFO @wdio/cli:launcher: Run onComplete hoo

提前感谢

推荐答案

代码的主要问题是,您对browser.execute()的输入在浏览器上下文中不起作用.它将无法访问您的webdriverio查询的元素.相反,您应该传递在浏览器上下文中工作的内容.因此,您必须传递 Select 器,而不是所选元素.

  async highlightById(element) {
    await browser.execute((arg) => {
      document.querySelector(arg).style.backgroundColor = 'red';
    }, element);
  }
await Utilities.highlightById('#username');
or
await Utils.highlightById(`#${await LoginPage.getUsername.getAttribute('id')}`);

Javascript相关问答推荐

单击按钮后未清除 Select

橡皮擦完全让画布变成白色

fs. writeFile()vs fs.writeFile()vs fs.appendFile()

react/redux中的formData在expressjs中返回未定义的req.params.id

Javascript,部分重排序数组

docx.js:如何在客户端使用文档修补程序

在我的html表单中的用户输入没有被传送到我的google表单中

并不是所有的iframe都被更换

制作钢琴模拟器,并且在控制台中不会执行或显示该脚本

如果Arrow函数返回函数,而不是为useEffect返回NULL,则会出现错误

如何在ASP.NET项目中使用Google Chart API JavaScript将二次轴线值格式化为百分比

使用js构造一个html<;ath&>元素并不能使其正确呈现

使用VUE和SCSS的数字滚动动画(&;内容生成)

将异步回调转换为异步生成器模式

try 使用PM2在AWS ubuntu服务器上运行 node 进程时出错

如何确保预订系统跨不同时区的日期时间处理一致?

在表单集中保存更改时删除';禁用';

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

如何根据查询结果重新排列日期

使用Perl Selify::Remote::Driver执行Java脚本时出错