Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when calling findElement after findElements on webdriver.WebElement (using Protractor, JS)

I'm using Protractor (Angular JS's webdriver wrapper), and although I can chain findElement indefinitely off of a single WebElement instance, I get an error when I attempt to chain findElement after using findElements (plural).

Error (stack trace given at bottom):

TypeError: Object [object Object] has no method 'findElement'

Chaining findElement:

var elementPromise = browser.findElement(by.css('body')).findElement(by.css('ul')).findElement(by.css('li'));
elementPromise.findElement(by.css('.icon-meter')); // does not raise error

Using findElement after findElements:

var arrayPromise = browser.findElement(by.css('ul')).findElements(by.css('li'));
elementPromise = arrayPromise.then(function(elems) {
    return elems[0];
});
elementPromise.findElement(by.css('.icon-meter')); // => raises error

Error message with trace

TypeError: Object [object Object] has no method 'findElement'
at repl:1:16
at /home/markham/src/dataraptor-rails4/spec/node_modules/elementexplorer-convenience.js:83:19
at webdriver.promise.ControlFlow.runInNewFrame_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1598:20)
at webdriver.promise.ControlFlow.runEventLoop_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1463:8)
at wrapper [as _onTimeout] (timers.js:252:14)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
like image 287
JellicleCat Avatar asked Jun 06 '14 17:06

JellicleCat


People also ask

How do you use findElement () and findElements ()?

The methods findElements and findElement are used to identify elements on a webpage. While findElement can pinpoint only one element, the findElements method yields a list of matching web elements. The return type of findElements is a list whereas the return type of findElement is a WebElement.

What is the difference between driver findElement () and driver findElements () commands?

driver. findElement() is used to find a webElement on a webpage. driver. findElements() is used to find a List of webElements matching the locator passed as parameter.

What will Driver findElements () return?

FindElement would return you the first element within the web page which matches the locator value.

What is the difference between findElements and findElement?

findElement: This command is used to uniquely identify a web element within the web page. findElements: This command is used to uniquely identify the list of web elements within the web page.


1 Answers

I got the same problem with the version 0.24 of protractor.

As a workaround, i have replaced all the invocation of findElement() by element() and all findElements() by all().

Chaining element:

var elementPromise = browser.element(by.css('body')).element(by.css('ul')).element(by.css('li'));
elementPromise.element(by.css('.icon-meter')); 

Using findElement after findElements:

var arrayPromise = browser.element(by.css('ul')).all(by.css('li'));
elementPromise = arrayPromise.then(function(elems) {
    return elems[0];
});
elementPromise.element(by.css('.icon-meter'));

There is more details on this release in this g+ post

like image 191
gontard Avatar answered Sep 30 '22 16:09

gontard