最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Protractor: How to return element by executing js script? - Stack Overflow

programmeradmin2浏览0评论

I have the goal to return drop-down list of elements. I tried to do this with protractor's methods, but I hadn't found easy ways for searching isolate-span elements. On that reason I want to use javasript code:

var my_js_element = browser.executeScript(jQuery("td.ng-binding>div.b-bobox.ps-list-drop-single-autoplete.ng-isolate-scope.ng-pristine.ng-required.ng-invalid.ng-invalid-required").isolateScope().psListDrop.toggleVisible(true).element);

But it isn't working. And I'm not sure that I can return elements with this method. Is it true? Or maybe somebody know how I can do this?

I have the goal to return drop-down list of elements. I tried to do this with protractor's methods, but I hadn't found easy ways for searching isolate-span elements. On that reason I want to use javasript code:

var my_js_element = browser.executeScript(jQuery("td.ng-binding>div.b-bobox.ps-list-drop-single-autoplete.ng-isolate-scope.ng-pristine.ng-required.ng-invalid.ng-invalid-required").isolateScope().psListDrop.toggleVisible(true).element);

But it isn't working. And I'm not sure that I can return elements with this method. Is it true? Or maybe somebody know how I can do this?

Share Improve this question asked Jul 16, 2015 at 11:48 Лилия СапуринаЛилия Сапурина 6277 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

According to the docs of browser.executeScript:

If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken for resolving this functions return value: - For a HTML element, the value will resolve to a webdriver.WebElement.

From your executeScript call you should return an HTML element, also it should be a "native" DOM element, so it could be converted to webdriver.WebElement. Then this element is resolved via promises and is available as an argument in a callback for .then():

browser.executeScript(function () {

    var element = jQuery('.world').get(0); // get "native" DOM node

    return element; // explicit return

}).then(function (webElement) {

    expect(webElement.getText()).toContain('Hello');

});

So Protractor is built ontop of the WebDriver specification.

According to the specification, it is possible to return values from executed scripts. It just might be JSON which would require you to convert it back. You can read more here.

Try logging the value it returns. You might also explore using an XPATH selector to find your element.

Something like:

//td[class="ng-binding"]/div[class="b-bobox" and class="ps-list-drop ...]

发布评论

评论列表(0)

  1. 暂无评论