WebDriverIO select using elements index

2019-08-28 17:08发布

问题:

I am using WebDriverIO to try to access (ie. getText, getAttribute, click, etc) an element after creating a list of elements. I am easily able to implement this element if I am using the browser.element() method, but the moment I use browser.elements(), I cannot access the individual objects in the array. According to the WebDriverIO docs, I should be able to access them using the value property.

Here is my pseudo-code. I assumed that these two functions should return the same thing:

usingElement() {
  return browser.element('.someCss');
}

usingElements() {
  return browser.elements('.someCss').value[0];
}

When I try to use the first block of code, it works perfectly fine.. but when I try to use the second block, it gives me an error saying usingElements.click is not a function or usingElements.getText is not a function, etc.

How can I isolate a single element object after using the browser.elements() method?

回答1:

I guess you might need to use one of the below two ways:

Way 1:

var elmnts = browser.elements('.someCss');
var element = elmnts.value[0].ELEMENT;
browser.elementIdClick(element);

Way 2:

var element = $$('.someCss')[0];
element.click();

Thanks, Naveen



回答2:

Your index reference was placed in the wrong spot. Try:

var myElement = browser.elements('.someCss')[0];
myElement.click();

You don't need to reference the value property, as WebdriverIO is smart enough to infer that for you.