Protractor: how to find an element by the exact text?

application testing ran into another problem. I need to click the element with exact text, so I found a solution in which I use cssContainingText and each, and then if the statment converts the text to text and selects a specific element, but I think that maybe this is the best solution for this magenta?

Second question: somewhere in stackoverflow I read that

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

will cause "dosomething"; will work after clicking, I try this and it does not work, how to do it "dosomething"; will be executed after clicking;

+8
protractor
source share
3 answers

You can use by.xpath as a selector:

Exact text

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

Css class with exact text

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

You can find many examples of xpath online . You can also use tester .

+16
source share

1) Delian has the correct answer, but in many cases the content of the element has extra spaces, 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/questions/106737/ ...

+4
source share

Xpath is elegant, but not so easy to read if someone else needs to look at the code. I often select an element in a repeater using cssContainingText and clained by.css only. Example: element(by.css('.dropdown-menu')).element(by.cssContainingText('span', constants.STATUS.PENDING)).click(); sure this can be slower than xpath. I really do not. But first controlled

0
source share

All Articles