量角器:如何通过执行js脚本返回元素? [英] Protractor: How to return element by executing js script?
问题描述
我的目标是返回元素的下拉列表.我试图用量角器的方法做到这一点,但我没有找到搜索跨度元素的简便方法. 因此,我想使用javasript代码:
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-combobox.ps-list-drop-single-autocomplete.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?
推荐答案
According to the docs of browser.executeScript
:
如果脚本具有返回值(即,如果脚本包含return语句),则将采取以下步骤来解决此函数的返回值:-对于HTML元素,该值将解析为webdriver.WebElement
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.
在您的executeScript
调用中,您应该返回一个HTML元素,它也应该是一个本机" DOM元素,因此可以将其转换为
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');
});
这篇关于量角器:如何通过执行js脚本返回元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!