Selector.withAttribute Method

Finds elements with the specified attribute or attribute value.

Selector().withAttribute(attrName [, attrValue]) → Selector
Argument Type Description
attrName String | RegExp Attribute name (case-sensitive).
attrValue (optional) String | RegExp Attribute value (case-sensitive, optional).

If the argument is a string, the method only returns strict matches.


// Selects label elements that have the 'for' attribute.
// This attribute can have any value.
const elWithAttrName = Selector('label').withAttribute('for');

// Selects label elements whose 'for' attribute
// is set to 'background-parallel-testing'. Does not match
// the 'class' attribute, or the 'for' attribute
// with the 'continuous-integration-embedding' value.
const elWithAttrNameAndValue = Selector('label').withAttribute('for', 'background-parallel-testing');

// Selects input elements that have an attribute whose
// name matches the /data-/ regular expression.
// This attribute must have a value that matches
// the /in/ regular expression.
// Matches the 'data-testid' attributes with the
// 'Windows' and 'Linux' values.
// Does not match the 'name' or 'value' attribute,
// as well as any attribute with the 'macos-radio'
// or 'tried-testcafe-checkbox' value.
const elWithRegExpAttr = Selector('input').withAttribute(/data-/, /in.*-radio/);