VLearnVibium

How to Get an Element Attribute in Vibium

Get an element attribute in Vibium with el.attr(). Runnable Python examples for reading href, src, data-* attributes, and asserting on them.

By Pramod Dutta··2 min read·Verified with Vibium 26.2
▶ Animated overview · made with Remotion

To get an element's attribute in Vibium, find the element and call attr() with the attribute name, like vibe.find("a").attr("href"). Vibium reads the attribute straight from the DOM and returns its value as a string, or None when the attribute is not present. This works for any HTML attribute — href, src, id, class, type, aria-*, and data-* custom attributes — using the same single method, so there is nothing special to learn for data attributes. Note the distinction from value(): attr() reads the attribute as written in the markup, while value() reads the live current value of a form input, which changes as a user types. (attr() is Vibium's idiom for what Playwright calls getAttribute().) Reading attributes is the backbone of scraping links, image sources, and metadata, and of asserting that markup is correct in tests.

How do I read an attribute of an element?

Locate the element with find(), then call attr() and pass the attribute name. Vibium returns its value as a string.

from vibium import browser_sync as browser
 
vibe = browser.launch()
vibe.go("https://example.com")
 
link = vibe.find("a")
print(link.attr("href"))     # e.g. "https://www.iana.org/domains/example"
 
img = vibe.find("img")
print(img.attr("src"))
print(img.attr("alt"))
 
vibe.quit()

Because find() auto-waits, the element exists by the time you read it, so you do not need to guard attr() with a separate existence check for markup that loads slightly after the page.

How do I read a data attribute?

Pass the full attribute name, including the data- prefix, to attr(). Custom data attributes are read exactly like standard ones.

from vibium import browser_sync as browser
 
vibe = browser.launch()
vibe.go("https://example.com/products")
 
card = vibe.find(".product-card")
product_id = card.attr("data-product-id")
print(product_id)
 
vibe.quit()

There is no special helper for data attributes — attr("data-product-id") returns the string value just like attr("href") would. If the attribute is missing, you get None, which you can check before using the value.

How do I assert on an attribute in a test?

Read the attribute with attr() and compare it using ordinary Python. Since the return value is a normal string (or None), any assertion style works.

from vibium import browser_sync as browser
 
vibe = browser.launch()
vibe.go("https://example.com/login")
 
submit = vibe.find(role="button", text="Log in")
assert submit.attr("type") == "submit"
 
# Substring and None checks work too
href = vibe.find("a.docs").attr("href")
assert href is not None and "/docs" in href
 
vibe.quit()

To read many attributes at once — for example every link's href — combine findAll() with attr() in a loop, since findAll() returns its matches as a list immediately.

Next steps

Frequently asked questions

How do I get an element attribute in Vibium?

Find the element with find(), then call attr() with the attribute name, like vibe.find('a').attr('href'). Vibium reads the attribute from the DOM and returns its value as a string, or None when the attribute is not present on the element.

What is the difference between attr() and value() in Vibium?

attr() reads any HTML attribute as written in the markup, such as href, src, or data-id. value() reads the live current value of a form input, which can change as the user types. Use value() for what is typed into a field and attr() for static markup attributes.

How do I read a data attribute in Vibium?

Pass the full attribute name to attr(), including the data- prefix, for example attr('data-product-id'). Vibium returns the attribute's string value, so a data-* attribute is read exactly like any other attribute with no special syntax or helper required.

Vibium is created by Jason Huggins. This is an independent tutorial — see the official Vibium site and GitHub repo for canonical docs.

Related guides