量角器:我如何获得下一个标签

时间:2015-01-07 06:27:39

标签: angularjs testing jasmine protractor end-to-end

是否有可能在另一个元素之后获得一个元素? (不是子元素)

HTML code:

<input id="first-input" type="text"/>
<ul>
    <li>1</li>
    <li>2</li>
</ul>
<input id="second-input" type="text"/>
<ul>
    <li>1</li>
    <li>2</li>
</ul>
<input id="third-input" type="text"/>
<ul>
    <li>1</li>
    <li>2</li>
</ul>

如何在ul之后获得element(by.id('second-input'))

以下是我的理解:

element(by.id('second-input')).element(by.tagName('ul')) // This doesn't work because this looks for sub-elements in <input id="second-input">
element.all(by.tagName('ul')).first() // This doesn't work because it gets the 'ul' following <input id="first-input">
element(by.id('second-input')).element.all(by.tagName('ul')) // This doesn't work because this looks for all 'ul' elements as sub-elements of <input id="second-input">

就我而言,这些人没有独特的ID,或者任何独特的东西。

2 个答案:

答案 0 :(得分:9)

您可以使用by.xpath()并要求跟随ul兄弟

element(by.id('second-input')).element(by.xpath('following-sibling::ul'));

或者,一气呵成:

element(by.xpath('//input[@id="second-input"]/following-sibling::ul'));

另一种选择是将by.css()adjacent sibling selector

一起使用
element(by.css('#second-input + ul'));

答案 1 :(得分:0)

使用Xpath选择器不是一个更好的选择,因为它会减慢元素查找机制的速度。

使用protractor-css-booster插件将非常简单。该插件将帮助您使用CSS选择器查找兄弟元素。

下载插件

npm install --save protractor-css-booster

在您的配置文件中添加插件:

exports.config = {
  // ... the rest of your config
  plugins: [
    {
      // The module name
      package: "protractor-css-booster"
    }
  ]
};

现在,您可以找到类似的元素

const ulElem=element(by.css('#second-input')).element(by.followingSibling('ul')); \\ returns the first div
相关问题