我正在使用Protractor(Angular JS的webdriver包装器),虽然我可以无限期地从一个WebElement实例链接findElement,但是在使用findElements(复数)后尝试链接findElement时出现错误.
TypeError: Object [object Object] has no method ‘findElement’
链接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
在findElements之后使用findElement:
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
带跟踪的错误消息
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)
解决方法
我的量角器版本0.24也遇到了同样的问题.
作为一种解决方法,我已经用element()替换了所有findElement()的调用,并且用all()替换了所有findElements().
链接元素:
var elementPromise = browser.element(by.css('body')).element(by.css('ul')).element(by.css('li')); elementPromise.element(by.css('.icon-meter'));
在findElements之后使用findElement:
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'));
此版本中有更多详细信息