11

testing applications encountered another problem. I need to click element by exact text, so i found a solution where I use cssContainingText and each,then if statment to comapre text and select certain element, but I think maybe is a better solution for that purpous?

Second questions: somewhere on stackoverflow I read that

element.click().then(){
dosomething;
});

will cause that 'dosomething;' will perform after click, I try this and it doesn't work, how to make that 'dosomething;' will perform after click;

tealang
  • 201
  • 1
  • 3
  • 11

4 Answers4

20

You can use by.xpath as a selector:

By exact text

element(by.xpath('.//*[.="text"]'))

By css class with exact text

element(by.xpath('.//*[.="text" and class="some-css-class"]'))

You can find a lot of xpath examples online. There's also a tester you can use.

Delian Mitankin
  • 3,691
  • 26
  • 24
  • you solution give me the same result as I have, If I have element with name like button1 and button12 it will find button1 and button12. I want find element by exact text but only one – tealang Jun 25 '15 at 08:18
  • It should be working. Please post your html and selectors – Delian Mitankin Jun 25 '15 at 08:25
  • i don't know that you understand me correctly. for example. I have two buttons with text: button1 and button12. If i write element(by.xpath('.//*[.="button1"]')) I only want find button1 – tealang Jun 25 '15 at 10:11
  • That's exactly what should happen and precisely how it works on my end. – Delian Mitankin Jun 25 '15 at 10:14
5

1) Delian has correct answer, but in many cases the element content has additional whitespaces, so you need to trim them:

getElementsByText: function (text, parentXpathSelector) {
    parentXpathSelector = parentXpathSelector || '//';
    return element(by.xpath(parentXpathSelector + '*[normalize-space(text())="' + text + '"]'));
}

2) https://stackoverflow.com/a/21785088/3147680

Arnaud P
  • 12,022
  • 7
  • 56
  • 67
Martin Sznapka
  • 829
  • 11
  • 6
0

Xpath is elegant but not so easy to read if someone else needs look at the code. I often pick an element in a repeater by only using cssContainingText and chained by.css. example:element(by.css('.dropdown-menu')).element(by.cssContainingText('span', constants.STATUS.PENDING)).click(); sure it might be slower than xpath. I realy don't now. But managable comes first

Jens Alenius
  • 1,931
  • 2
  • 16
  • 20
  • 1
    That works well except for when you have multiple items and one is a substring of another. `cssContainingText` will match both. – Stiggler Apr 13 '17 at 15:46
0
 const allOptions = element.all(by.css('css-class'));
            allOptions.filter( (elem) => {
                return elem.getText().then((text) => {
                    return exact-text === text;
                });
            }).first().click();