Simulates a click event on the given DOM element. The element is scrolled into view if it is not already pointer-interactable. See the WebDriver specification for element interactability.

The command click() will automatically wait for the element to be present (until the specified timeout). If the element is not found, an error is thrown which will cause the test to fail. You can suppress element not found errors by specifying the selector argument as an object and passing the suppressNotFoundErrors = true option.

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

Usage

                    .click(selector, [callback])
                
                    .click(using, selector, [callback])
                
                    browser.element(selector).click()
                

Example

module.exports = {
  demoTest(browser) {
    browser.click('#main ul li a.first');

    browser.click('#main ul li a.first', function(result) {
      console.log('Click result', result);
    });

    // with explicit locate strategy
    browser.click('css selector', '#main ul li a.first');

    // with selector object - see https://nightwatchjs.org/guide#element-properties
    browser.click({
      selector: '#main ul li a',
      index: 1,
      suppressNotFoundErrors: true
    });

    browser.click({
      selector: '#main ul li a.first',
      timeout: 2000 // overwrite the default timeout (in ms) to check if the element is present
    });
  },

  demoTestAsync: async function(browser) {
    const result = await browser.click('#main ul li a.first');
    console.log('Click result', result);
  }
}

Parameters

Name Type description
using
Optional
string

The locator strategy to use. See W3C Webdriver - locator strategies

selector string

The CSS/Xpath selector used to locate the element.

callback
Optional
function

Optional callback function to be called when the command finishes.

W3C WebDriver spec