简体   繁体   English

如何使用Webdriverio遍历JSON元素列表并访问其属性?

[英]How to iterate through a list of JSON elements and access their attributes using webdriverio?

[Using Chimp.js – Synchronous style webdriverio API] [使用Chimp.js –同步样式的webdriverio API]

How can I properly iterate through my array of elements? 如何正确遍历元素数组? Or, more specifically, how do I access the attributes of the elements themselves? 或者,更具体地说,如何访问元素本身的属性? I'm confused as to the .elements() function found in the API and how to extract the elements themselves from there. 对API中.elements()函数以及如何从中提取元素本身感到困惑。

var myItem;
var elemArray = browser.elements('.castMemberPicture').value;
console.log(elemArray);

for (myItem in elemArray){ 
    console.log("myItem: " + myItem);
    //  I can log the JSON obj IDs successfully, but can’t seem to access elements like clientHeight, alt, ...
};

How do I access the attributes? 如何访问属性?

(output) (输出)

[ { ELEMENT: '0' },
  { ELEMENT: '1' },
  { ELEMENT: '2' },
  { ELEMENT: '3' }]
myItem: 0
myItem: 1
myItem: 2
myItem: 3

... calling to .ELEMENT gives undefined calls, so it's likely my use of the API / syntax. ...调用.ELEMENT会产生未定义的调用,因此很可能是我使用的API /语法。

I saw https://github.com/webdriverio/webdriverio/issues/273 but I can't get to access the attributes no matter what combination of .ELEMENT .value and function I try. 我看到了https://github.com/webdriverio/webdriverio/issues/273,但是无论我尝试使用.ELEMENT .value和函数的哪种组合,都无法访问属性。 Help? 救命?

note - if I try to explore the elements themselves by printing using console.log("myItem: " + JSON.stringify(elemArray[myItem].ELEMENT)); 注意-如果我尝试通过使用console.log("myItem: " + JSON.stringify(elemArray[myItem].ELEMENT));打印来探索元素本身console.log("myItem: " + JSON.stringify(elemArray[myItem].ELEMENT)); the output becomes 输出变为

[ { ELEMENT: '0' },
  { ELEMENT: '1' },
  { ELEMENT: '2' },
  { ELEMENT: '3' }]
myItem: "0"
myItem: "1"
myItem: "2"
myItem: "3"

Here's the way to traverse it and check the elements in Chimp: 这是遍历并检查Chimp中元素的方法:

var myItem;
var elemArray = browser.elements('.castMemberPicture').value;
console.log(elemArray);

// the following will traverse the array and print out the elements 'alt' Attribute!
for (myItem in elemArray){ 
    console.log( browser.elementIdAttribute(myItem, 'alt').value);
};

According to the docs for 'elements', you can access each element individually using the element command: 根据有关“元素”的文档,您可以使用element命令单独访问每个元素:

The array of elements can be retrieved using the 'response.value' which is a collection of element ID's and can be accessed in the subsequent commands using the '.ELEMENT' method. 可以使用“ response.value”检索元素数组,该元素是元素ID的集合,并且可以在后续命令中使用“ .ELEMENT”方法进行访问。

So for your loop: 因此,对于您的循环:

var myItem;
var elemArray = browser.elements('.castMemberPicture').value;

for (myItem in elemArray){ 
    console.log( browser.getAttribute(myItem, 'alt'));
};

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

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