Locate element by a presence of a custom attribute

2019-09-20 17:35发布

问题:

How do you locate all those elements that has particular custom attribute in Protractor? I found similar questions on Stackoverflow and on net, but they uses xpath which puts restriction on tag.

element(by.xpath('//div[@custom-attribute]'))

As oppose to above example, I don't want to put restriction on tag since we have different tags with the same custom attribute. I'd like locate all elements with the attribute regardless of tag. Is that possible?

回答1:

You can use a CSS selector locator:

element.all(by.css('[custom-attribute]'));

Or, via the $$ shortcut:

$$('[custom-attribute]');

[custom-attribute] is an attribute selector that would match any element having custom-attribute attribute.