[英]How do you find the next element in Protractor?
Given I have the following HTML code:鉴于我有以下 HTML 代码:
<div class="someGenericClass">
<input name="someGenericName">
<label>not important</label>
</div>
<div class="someGenericClass">
<input name="someGenericName">
<label>not important</label>
</div>
<div class="someGenericClass">
<input name="someGenericName">
<label>Text I need to find in my test</label>
</div>
How do I find the label text in this case?在这种情况下如何找到 label 文本? It has no attribute.
它没有属性。 I tried to find the first element, and then the next element, but I am missing something:
我试图找到第一个元素,然后是下一个元素,但我遗漏了一些东西:
element(by.name('someGenericName')).element(by.xpath('following::label'));
Also tried:也试过:
element(by.name('someGenericName')).element(by.xpath('following-sibling::label'));
EDIT: The class is not unique.编辑: class 不是唯一的。 It is used multiple times in the DOM.
它在 DOM 中多次使用。
What happens if the DOM will change?如果 DOM 发生变化会发生什么?
element.all
seems like a bad idea then, right? element.all
似乎是个坏主意,对吧?
element.all should work fine element.all 应该可以正常工作
HTML View
<div id='id1' class="parent">
<ul>
<li class="foo">1a</li>
<li class="baz">1b</li>
</ul>
</div>
<div id='id2' class="parent">
<ul>
<li class="foo">2a</li>
<li class="bar">2b</li>
</ul>
</div>
Code
let foo = element.all(by.css('.parent')).all(by.css('.foo'));
expect(foo.getText()).toEqual(['1a', '2a']);
let baz = element.all(by.css('.parent')).all(by.css('.baz'));
expect(baz.getText()).toEqual(['1b']);
let nonexistent = element.all(by.css('.parent'))
.all(by.css('.NONEXISTENT'));
expect(nonexistent.getText()).toEqual(['']);
// Or using the shortcut $$() notation instead of element.all(by.css()):
let foo = $$('.parent').$$('.foo');
expect(foo.getText()).toEqual(['1a', '2a']);
let baz = $$('.parent').$$('.baz');
expect(baz.getText()).toEqual(['1b']);
let nonexistent = $$('.parent').$$('.NONEXISTENT');
expect(nonexistent.getText()).toEqual(['']);
element.all returns a promise array. element.all 返回一个 promise 数组。 You can iterate array and chain to find the label which is following the input tag.
您可以迭代数组和链以找到输入标签之后的 label。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.