Skip to main content

getAttribute

Overview

Use the getAttribute command to retrieve an attribute from a DOM element based on the attribute name.

Usage

await browser.$(selector).getAttribute(attributeName);

Command Parameters

NameTypeDescription
attributeNameStringThe name of the attribute to retrieve.

Usage Examples

index.html

<form action="/submit" method="post" class="loginForm">
<input type="text" name="name" placeholder="username"></input>
<input type="text" name="password" placeholder="password"></input>
<input type="submit" name="submit" value="submit"></input>
</form>

getAttribute.js

it("should demonstrate the getAttribute command", async ({ browser }) => {
const form = await browser.$("form");
const attr = await form.getAttribute("method");
console.log(attr); // outputs: "post"
});

References

We'd like to give credit to the original WebdriverIO docs article, from which we drew some information while writing our version.