Search for an element on the page, starting from the document root. The located element will be returned as a web element JSON object.
First argument to be passed is the locator strategy, which is detailed on the WebDriver docs.

The locator strategy can be one of:

  • css selector
  • link text
  • partial link text
  • tag name
  • xpath
For more info on working with DOM elements in Nightwatch, refer to the Finding & interacting with DOM Elements guide page.

Usage

.element(using, value, [callback])

Example

module.exports = {
 'demo Test' : function(browser) {
    browser.element('css selector', 'body', function(result) {
      console.log(result.value)
    });
  },

  'es6 async demo Test': async function(browser) {
    const result = await browser.element('css selector', 'body');
    console.log('result value is:', result.value);
  }
}

// Example with using page object elements
module.exports = {
 'demo Test with page object' : function(browser) {
    const loginPage = browser.page.login();
    loginPage.api.element('@resultContainer', function(result) {
      console.log(result.value)
    });
  }
}

Parameters

Name Type description
using string

The locator strategy to use.

value string

The search target.

callback function

Callback function which is called with the result value.

W3C WebDriver spec