Selector.withAttribute Method

Finds elements that contain the specified attribute and, optionally, attribute value.

Selector().withAttribute(attrName [, attrValue]) → Selector
Argument Type Description
attrName String | RegExp The attribute name. This parameter is case-sensitive.
attrValue (optional) String | RegExp The attribute value. This parameter is case-sensitive. You can omit it to select elements that have the attrName attribute regardless of the value.

If attrName or attrValue is a string, withAttribute uses strict match to select an element.

Examples

// 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/);