5

html:

<button class="helperButton ng-scope" ng-if="!isDisabled && displayHelper( 'none' )" ng-click="select( 'none', $event );" type="button">  ×  Select None</button>

trying to locate with:

element(by.css('button[ng-click="select( \'none\', $event );"]'));

get error:

Failed: No element found using locator: By.cssSelector("button[ng-click=\"select( 'none', $event );\"]")

this link provides a solution AngularJS Protractor - Finding an Element on a Page Using Ng-Click Binding but does not have details to the function its referencing. Any help would be appreciated.

1 Answer 1

4

Having your locator based on the ng-click is not quite reliable and readable.

Instead I would rely on the button text:

by.xpath('//button[contains(., "Select None")]')

or, on the class:

by.css('button.helperButton')

Or, if have a control over the application HTML templates - add an id attribute and use:

by.id('mybuttonid')

If you still want to use ng-click - try using starts-with CSS selector syntax to check ng-click attribute:

by.css('button[ng-click^=select]')
Sign up to request clarification or add additional context in comments.

10 Comments

I tried button text but it has that strange x character and all those spaces that makes it not work.
@awaken well, that's why I've used contains(). It is strange that you still cannot locate it. Are there any frame or iframe elements on the page? Thanks.
the page has tons of elements that meet those locating methods and I will get WARNING - more than one element found for locator By.xpath("//button[contains(., \"Select None\")]") - the first result will be used thats why I'm using ng-click
@awaken well, if there are many elements with Select None text inside - I suspect ng-click would also be the same for them. How do you know which one do you need? Thanks.
xpath //*[@id='-search-form']/div[1]/div[4]/div[1]/div[1]/div/span/div/form/div[1]/div[1]/button[2] works but I try to stay away from xpaths because they can be brittle.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.