6

Is it possible to get the next sibling by using by.cssContainingText()

Example: HTML code is like below:

<div ng-repeat="SomeNgRepeat" class="ng-scope">
    <div class="text-label" >SomeText</div>
    <div class="some-class">SomeValue</div>
</div>

Get element by using:

element(by.cssContainingText('div.text-label','SomeText'))

Now find the next sibling of the above element.

I know of css=form input.username + input way of finding the sibling. However, this is not working in my case!

I think 'chaining' can be used to achieve this, but don't know How!

Thanks, Sakshi

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Sakshi Singla
  • 2,462
  • 1
  • 18
  • 34

3 Answers3

10

What if you would get it in one go using by.xpath():

element(by.xpath('//div[@class="text-label" and . = "SomeText"]/following-sibling::div'))

Or, you can combine it with cssContainingText():

element(by.cssContainingText('div.text-label','SomeText')).element(by.xpath('following-sibling::div'))
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks alot alexce. This worked like magic :) Is there a link which contains all these following-sibling kindof css selectors? If not, then maybe you can list them out here. This will make life a lot easier :) – Sakshi Singla Feb 17 '15 at 03:47
  • 1
    @SakshiSingla glad it worked. As for xpath-axes related information, see, for example, [mozilla's developer docs](https://developer.mozilla.org/en-US/docs/Web/XPath/Axes). – alecxe Feb 17 '15 at 17:37
  • I want to add a 'text' search in the below xpath. Is there a possibility?(Since its non-angular, so chaining is not supported here!) browser.driver.findElements(by.xpath('//*[@id="some-id"]//tr/td[1]')); Something like: browser.driver.findElements(by.xpath('//*[@id="some-id"]//tr/td[1][.="sometext"]')); – Sakshi Singla Feb 19 '15 at 07:19
  • browser.driver.findElement(by.xpath('//*[@id="someId"]//tr/td[1]/self::td[.="'some_text'"]')) WORKED!! – Sakshi Singla Feb 19 '15 at 07:52
  • Its good in a way :) But anyways, thanks a lot for your responses. Those really help a lot :) – Sakshi Singla Feb 19 '15 at 07:58
  • Is it possible to get an element by substring? Like if an element has text 'I am here'. I want to match only 'here' substring. Something like: by.xpath('span[@id="ID" and .*="here"]')@alecxe: Is it possible to get an element by substring? – Sakshi Singla Feb 25 '15 at 08:15
  • Achieved using the following: by.xpath('//*[@id="ID"] [text()[contains(.," here")]]') – Sakshi Singla Feb 25 '15 at 08:35
  • 1
    @SakshiSingla you are solving it by yourself again :) Btw, you can simplify the expression a bit: `//*[@id = "ID" and contains(., " here")]`. – alecxe Feb 25 '15 at 13:19
0

You could use the node.nextSibling selector. Check out this article

Jackson
  • 3,476
  • 1
  • 19
  • 29
0

Using Xpath selectors is not a better choice as it slows down the element finding mechanism.

I have designed a plugin to address this specific issues: protractor-css-booster

The plugin provides some handly locators to find out siblings in a better way and most importantly with CSS selector.

using this plugin, you can directly use:

var elem = await element(by.cssContainingText('div.text-label','SomeText')).nextSibling();

or, use booster as by-locator

var elem = element(by.cssContainingText('div.text-label','SomeText')).element(by.followingSibling('div'));

Hope, it will help you...