hubcap attr

Get a specific attribute value from an element.

When to use

Use attr to read a single HTML attribute from the first element matching a selector. Use query to get all attributes of an element at once. Use computed to read CSS properties instead of HTML attributes.

Usage

hubcap attr <selector> <attribute>

Arguments

ArgumentTypeRequiredDescription
selectorstringYesCSS selector for the target element
attributestringYesName of the HTML attribute to read

Flags

None.

Output

FieldTypeDescription
selectorstringThe selector that was queried
attributestringThe attribute name that was read
valuestringThe attribute’s value, or empty string if not present
{"selector":"#link","attribute":"href","value":"https://example.com"}

Errors

ConditionExit codeStderr
Element not found1error: element not found: <sel>
Chrome not connected2error: connecting to Chrome: ...
Timeout3error: timeout

Examples

Get the href of a link:

hubcap attr 'a.logo' 'href'

Get the src of an image:

hubcap attr '#hero-img' 'src'

Check an element’s ARIA state:

hubcap attr '#menu' 'aria-expanded'

Read a data attribute then use it in a subsequent command:

hubcap attr '#product' 'data-id' | jq -r '.value' | xargs -I{} hubcap goto "https://example.com/api/products/{}"

See also

  • query - Get all attributes of an element at once
  • text - Get the inner text of an element
  • html - Get the full outer HTML of an element
  • computed - Get a computed CSS property value
  • value - Get the value of a form input