Retrieve the value of an attribute for a given DOM element.

For more info on working with DOM elements in Nightwatch, refer to the Finding & interacting with DOM Elements guide page.

Usage

                    browser.element(selector).getAttribute(name)
                

Example

export default {
  demoTest(browser: NightwatchAPI): void {
    browser
      .element('#main ul li a.first')
      .getAttribute('target')
      .assert.valueEquals('_blank');
  },

  async demoTestAsync(browser: NightwatchAPI): Promise<void> {
    const result = await browser.element('#main ul li a.first').getAttribute('href');
    console.log('attribute', result);
  }
}

Parameters

Name Type description
name string

The attribute name to inspect.

Returns

Type description
ScopedValue<string|null>

The value of the attribute

W3C WebDriver spec