简体   繁体   English

Nightwatch.js方法`.element()`返回“ [object Object]”

[英]Nightwatch.js method `.element()` returns “[object Object]”

I am attempting to retrieve an element from a webpage using the Nightwatch .element() method: 我正在尝试使用Nightwatch .element()方法从网页中检索元素:

browser.element('class name', 'story-share-tools', function (res) {  
  console.log(res);
});

The res parameter is being displayed in the console as [object Object] and doesn't appear to be a WebElement JSON object . res参数在控制台中显示为[object Object]并且似乎不是WebElement JSON对象

I need the actual HTML element story-share-tools that contains an unordered list with items I want to iterate over. 我需要实际的HTML元素story-share-tools ,其中包含一个包含要迭代的项目的无序列表。

It is a bit surprising because I do not have this problem with .element() : object details are displayed in my terminal. 有点令人惊讶,因为我对.element()没有此问题:对象详细信息显示在我的终端中。 I assume you have an issue with .toString() somewhere. 我假设您在某处有.toString()问题。 As you may know, in JavaScript, [object Object] is the string returned by Object.prototype.toString() . 您可能知道,在JavaScript中, [object Object]Object.prototype.toString()返回的字符串。

 var obj = { foo: 'Foo', bar: 'Bar' }; console.log(obj.toString()); 

So instead of doing this: 所以不要这样做:

console.log(res);

Try this: 尝试这个:

for (var prop in res) {
  if (res.hasOwnProperty(prop)) {
    console.log(prop, res[prop]);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM