Get Attribute task | Zoho QEngine Help

Get Attribute task

In a nutshell

The Get Attribute task retrieves the value of a named HTML attribute from a web element during test execution and stores it in a variable. This lets a script inspect things that aren't part of the visible text, such as an image's alt text or a link's destination, or check a value that stays fixed even when the element's displayed text changes.

1. Overview

The Get Attribute task retrieves the value of a named HTML attribute from a web element during test execution and returns it as a string that can be stored in a variable for a later check or comparison. The attribute can be a standard HTML attribute, such as href, src, alt, title, disabled, or value, or a custom data attribute defined by the page. This applies to values a page doesn't render as visible text, such as a data attribute used to track a product or record internally, or the presence of a disabled attribute indicating that a field isn't editable.
An element's attribute value and its displayed text aren't always the same. A stock-count data attribute can hold a raw numeric value while the page displays only "In Stock" or "Low Stock" text. Get Attribute reads that underlying value directly, regardless of what's currently shown on screen.

2. Syntax

<variable> = getAttribute(<locator>, <attribute>);


ParameterData TypeDescription
variableSTRINGStores the attribute value retrieved from the element.
locatorSTRINGIdentifies the element whose attribute value should be retrieved.
NotesNote: Locators can be fetched in one of two ways, either by manually inspecting the webpage to identify the element, or by using a recorded element saved during a recording session. Learn more about Elements.
attributeSTRINGName of the attribute to retrieve, such as alt, href, or a custom data attribute.


Return Type

STRING

3. Examples

Example 1: Confirming a product image's alt text contains the product name

Confirm the Blue Bay GMT product image's alt attribute contains the product name, rather than checking for an exact match, to support meaningful accessibility text.

// Open the Blue Bay GMT product page on the ZWatch Commerce website using the Open URL task
openURL("https://zwatch.zohocommerce.com/products/blue-bay-gmt/3531661000000075668", "same window");
// Read the product image's alt attribute using the Get Attribute task
altText = getAttribute(ui.zwatch.blue-bay-gmt-image, "alt");
// Confirm the alt text contains the product name using the conditional logic task
if (!altText.containsIgnoreCase("Blue Bay GMT"))
{
throwError("Product image alt text does not contain the expected product name"); //Learn more about Throw Error task
}

where:


"https://zwatch.zohocommerce.com/products/blue-bay-gmt/3531661000000075668"Blue Bay GMT product page URL.
"same window"Opens the page in the window already running the test.
ui.zwatch.blue-bay-gmt-imageStored locator for the product image.
"alt"Name of the attribute to retrieve.
altTextVariable storing the retrieved alt text.
"Blue Bay GMT"Expected product name the alt text must contain.

Example 2: Confirming a link's href resolves to the correct destination

Confirm the Contact link's href attribute resolves to the correct destination page.

// Open the ZWatch Commerce home page using the Open URL task
openURL("https://zwatch.zohocommerce.com/", "same window");
// Read the Contact link's href attribute using the Get Attribute task
contactHref = getAttribute("a[href='/contact']", "href");
// Confirm the href resolves to the correct destination using conditional logic tasks
if (contactHref != "https://zwatch.zohocommerce.com/contact")
{
throwError("Contact link href does not resolve to the expected page"); //Learn more about Throw Error task
}

where:

"https://zwatch.zohocommerce.com/"ZWatch Commerce home page URL.
"same window"Opens the page in the window already running the test.
a[href='/contact']Locator for the Contact link.
"href"Name of the attribute to retrieve.
contactHrefVariable storing the retrieved href value.
"https://zwatch.zohocommerce.com/contact"Expected resolved href value for the Contact link.

Example 3: Confirming the underlying price stays fixed across a currency change

Confirm the underlying price attribute holds the same fixed value both before and after switching the displayed currency to USD.
// Open the Diver Chronomat product page on the ZWatch Commerce website using the Open URL task
openURL("https://zwatch.zohocommerce.com/products/diver-chronomat/3531661000000075722", "same window");
// Read the underlying price attribute before switching currency using the Get Attribute task
priceBefore = getAttribute("[data-zs-selling-price]", "data-zs-selling-price");
// Click the currency dropdown using the Click task
click(".theme-currency-option-container");
// Select USD from the currency dropdown using the Click task
click(ui.zwatch.currency-usd-option);
// Read the underlying price attribute again using the Get Attribute task
priceAfter = getAttribute("[data-zs-selling-price]", "data-zs-selling-price");
// Confirm both readings match using the conditional logic task
if (priceAfter != priceBefore)
{
throwError("Underlying price attribute changed unexpectedly after switching currency"); //Learn more about Throw Error task
}

where:

"https://zwatch.zohocommerce.com/products/diver-chronomat/3531661000000075722"Diver Chronomat product page URL.
"same window"Opens the page in the window already running the test.
[data-zs-selling-price]Locator for the displayed price.
"data-zs-selling-price"Name of the attribute to retrieve.
priceBeforeVariable storing the price attribute read before switching currency.
.theme-currency-option-containerLocator for the currency selector.
ui.zwatch.currency-usd-optionStored locator for the USD option in the currency selector.
priceAfterVariable storing the price attribute read after switching currency.

Example 4: Reading a record's unique identifier in a Lease Tracker application

Confirm a lease record row's unique identifier in a Zoho Creator Lease Tracker application matches the expected value.
// Open the Leases report in a Zoho Creator Lease Tracker application using the Open URL task
openURL("https://creatorapp.zoho.com/<orgname>/lease-tracker#Report:Leases", "same window");
// Read the lease record's ID attribute using the Get Attribute task
recordId = getAttribute(ui.leasetracker.lease-record-row, "data-record-id");
// Confirm the record ID matches the expected value using conditional logic tasks
if (recordId != "4771000000123456")
{
throwError("Lease record ID does not match the expected value"); //Learn more about Throw Error task
}

where:


"https://creatorapp.zoho.com/<orgname>/lease-tracker#Report:Leases"URL of the Leases report in a Zoho Creator Lease Tracker application.
"same window"Opens the report in the window already running the test.
ui.leasetracker.lease-record-rowLocator for the lease record row.
"data-record-id"Name of the attribute to retrieve.
recordIdVariable storing the retrieved record ID.
"4771000000123456"Expected record ID for the lease record.

4. Use cases

Scenario 1: Confirming the product image has proper alt text for accessibility

Shoppers using screen readers rely on descriptive alt text to understand what a product image shows, and missing or generic alt text leaves them without that information entirely.
A tester reads the product image's alt attribute and confirms it names the actual product, rather than being empty or generic.
Steps
  1. Open the Blue Bay GMT product page in the same window.
  2. Read the product image's alt attribute and store the value in a variable.
  3. Confirm the alt text contains the product name.

Implementation

// Step 1: Open the Blue Bay GMT product page on the ZWatch Commerce website in the same window using the Open URL task
openURL("https://zwatch.zohocommerce.com/products/blue-bay-gmt/3531661000000075668", "same window");
// Step 2: Read the product image's alt attribute and store the value in a variable using the Get Attribute task
altText = getAttribute(ui.zwatch.blue-bay-gmt-image, "alt");
// Step 3: Confirm the alt text contains the product name using the conditional logic task
if (!altText.containsIgnoreCase("Blue Bay GMT"))
{
throwError("Product image alt text does not contain the expected product name"); //Learn more about Throw Error task
}
Scenario 2: Confirming the currency selector reflects USD after switching currencies

Shoppers switching the store's displayed currency expect the selector itself to reflect their choice, not just the prices shown elsewhere on the page. Confirming the visible label changed isn't enough on its own, since the selector also carries an underlying attribute that other parts of the page may rely on to track the active currency.
A tester switches the currency to USD and reads the currency selector's underlying attribute to confirm it reflects the new selection.
Steps
  1. Open the ZWatch Commerce home page in the same window.
  2. Click the currency selector to reveal the currency options.
  3. Select USD from the currency selector.
  4. Read the currency selector's underlying attribute and store the value in a variable.
  5. Confirm the attribute reflects the USD selection.

Implementation

// Step 1: Open the ZWatch Commerce home page in the same window using the Open URL task
openURL("https://zwatch.zohocommerce.com/", "same window");
// Step 2: Click the currency selector to reveal the currency options using the Click task
click(".theme-currency-option-container");
// Step 3: Select USD from the currency selector using the Click task
click(ui.zwatch.currency-usd-option);
// Step 4: Read the currency selector's underlying attribute and store the value in a variable using the Get Attribute task
selectedCurrency = getAttribute(".theme-currency-option-container", "data-currency-code");
// Step 5: Confirm the attribute reflects the USD selection using the conditional logic task
if (selectedCurrency != "USD")
{
throwError("Currency selector attribute does not reflect the USD selection"); //Learn more about Throw Error task
}
Scenario 3: Confirming a candidate's photo has proper alt text for accessibility in a Zoho Creator application

Consider a Zoho Creator Recruitment Portal application where a candidate's uploaded photo appears in their record. Recruiters using screen readers rely on descriptive alt text to identify whose photo they're viewing, and a missing or generic alt attribute leaves that identification out entirely.
A tester reads the candidate's photo alt attribute and confirms it names the specific candidate, rather than being empty or generic.
Steps
  1. Open the candidate's record in the Recruitment Portal application.
  2. Read the candidate's photo alt attribute and store the value in a variable.
  3. Confirm the alt text names the candidate.

Implementation

// Step 1: Open the Candidates report in a Zoho Creator Recruitment Portal application using the Open URL task
openURL("https://creatorapp.zoho.com/<orgname>/recruitment-portal#Report:Candidates", "same window");
// Step 2: Click the candidate's record to open it using the Click task
click(ui.recruitmentportal.candidate-row);
// Step 3: Read the candidate's photo alt attribute and store the value in a variable using the Get Attribute task
candidatePhotoAlt = getAttribute(ui.recruitmentportal.candidate-photo, "alt");
// Step 4: Confirm the alt text contains the candidate's name using the conditional logic task
if (!candidatePhotoAlt.containsIgnoreCase("Jordan Cole"))
{
throwError("Candidate photo alt text does not contain the expected candidate name"); //Learn more about Throw Error task
}

5. Points to note

  1. Use Get Attribute to retrieve a named HTML attribute from an element. If you need to retrieve the text displayed on the page instead, use the Get Text task.
  2. A locator only resolves against the currently active window, tab, or iframe. If the element sits in a different window, tab, or iframe, switch context first with the Switch To task.
  3. For the Get Attribute task, use a stable locator, such as an ID, name, or CSS attribute selector, with the Get Attribute task. Avoid full XPath locators for elements with dynamic attributes, such as prices or record identifiers, as they are more likely to break when the page is re-rendered.

6. Related topics

  1. Get Text
  2. Assert Value
  3. Get Count

7. What's next

Next step
Previous step
Next step

Now that you've learned how to retrieve an element's attribute value using the Get Attribute task, learn how to count the occurrences of an element using the Get Count task.

Previous step

Before using the Get Attribute task, ensure that you've learned how to retrieve an element's visible text using the Get Text task.