getAttribute

Get an attribute from a DOM-element based on the selector and attribute name. Returns a list of attribute values if selector matches multiple elements.

Usage

1
browser.getAttribute(selector,attributeName);

Parameters

Param Type Details
selector String element with requested attribute
attributeName String requested attribute

Example

index.html
1
2
3
4
5
<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
1
2
3
4
5
6
7
8
9
10
11
12
it('should demonstrate the getAttribute command', function () {
var form = $('form')

var attr = form.getAttribute('method')
console.log(attr) // outputs: "post"
// or
console.log(browser.getAttribute('form', 'method')) // outputs: "post"

// if your selector matches multiple elements it returns an array of results
var allInputs = $$('.loginForm input')
console.log(allInputs.map(function(el) { return el.getAttribute('name'); })) // outputs: ['name', 'password', 'submit']
})

Returns

  • <String/String[]/null>: The value of the attribute(s), or null if it is not set on the element.

Uses