Skip to content

Instantly share code, notes, and snippets.

@ivan-kleshnin
Created August 5, 2016 09:17
Show Gist options
  • Save ivan-kleshnin/814f6b9d7d0057cd1e9e5c33acf81312 to your computer and use it in GitHub Desktop.
Save ivan-kleshnin/814f6b9d7d0057cd1e9e5c33acf81312 to your computer and use it in GitHub Desktop.
NightmareJS `clickNth` custom action
Nightmare.action("clickNth", function (selector, n, done) {
debug(".clickNth() on " + selector + ":" + n)
this.evaluate_now(function (selector, n) {
document.activeElement.blur()
var element = document.querySelectorAll(selector)[n]
if (!element) {
throw new Error('Unable to find element by selector: ' + selector)
}
var event = document.createEvent('MouseEvent')
event.initEvent('click', true, true)
element.dispatchEvent(event)
}, done, selector, n)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment