我最熟悉java selenium,我是JS和Protractor的新手。假设我试图从具有公共标识符的选项列表中单击选项..
<a title=" <%= request.getAttribute("title").toString().replace("],", "\n") %>"></a>
我怎样才能获得具有该公共标识符的所有元素,然后通过其文本选择一个?我无法在java中执行driver.findElements,因为没有对驱动程序的引用..
这是我到目前为止所尝试的但它不起作用,我认为这是由于我对JS的经验不足
var options = $('.options');
答案 0 :(得分:3)
选择具有公共标识符的所有元素:$$('.options');
选择类.options
的所有元素 - 相当于element.all(by.css('.options'))
。这会返回ElementArrayFinder。另请参阅.get()了解如何从ElementArrayFinder中按索引选择元素。
按文字查找,您可以使用cssContainingText(css, text)
。例如,
var loginBtn = element(by.cssContainingText('button.ng-scope', 'Login'));
但是如果由于某些原因那些没有提供预期的结果,你可以在ElementArrayFinder上使用.filter()
(docs here)来遍历元素数组并根据条件找到一个元素指定。例如,
var allOptions = $$('.options');
allOptions.filter(function (elem) {
return elem.getText().then(function (text) {
return text === 'What you want';
});
}).first().click();
而且,虽然我从未使用过常规的Java Selenium(所以我不知道这是否相同),但确实存在browser
引用(因此{{1}功能):http://www.protractortest.org/#/api?view=ProtractorBrowser。
希望它有所帮助!
修改强>
使用您的代码:
findElements
第二次修改 假设公司代码是唯一的,您可能不需要使用过滤器。试试这个:
this.selectCompanyCode = function(companyCode) {
// where is dropDownMenus defined? This has function no reference to it.
dropDownMenus.get(0).click(); // should be this
var companyCodeOptions = $$('[ng-bind-html="' + companyCode + '"]');
return companyCodeOptions.filter(function (elem) {
return elem.getText().then(function text() {
return text === companyCode;
});
}).first().click();
};
答案 1 :(得分:0)
使用cssContainingText
element(by.cssContainingText(".option", "text")).click();
http://www.protractortest.org/#/api?view=ProtractorBy.prototype.cssContainingText