Reputation: 1
I cannot make nightmareJS to click the search button, although .type() works well. What could be the issue?
.type('form[action="/search/web/direct_search.php"] [class=_1frb]', string)
.click('form[action="/search/web/direct_search.php"] [type=submit]')
//.click('#js_x > form > button')
//.click('button[class="_42ft _4jy0 _4w98 _4jy3 _517h _51sy"]')
Developer Tools console finds the button:
document.querySelector('form[action="/search/web/direct_search.php"] [type=submit]')
<button value="1" class="_42ft _4jy0 _4w98 _4jy3 _517h _51sy" aria-label="Search" tabindex="-1" data-testid="facebar_search_button" type="submit">…</button>
Later Edit:
There you go, my friend, I've removed the import and put everything in a single file, I'm already logged in with the cookie, so this is the full code that replicates the issue:
var Nightmare = require('nightmare');
var nightmare = Nightmare({
show: true
});
var string = "TEST";
var search = function(string) {
return function(nightmare) {
nightmare
.goto('http://www.facebook.com')
.wait('form[action="/search/web/direct_search.php"] [class=_1frb]')
.type('form[action="/search/web/direct_search.php"] [class=_1frb]', string)
.click('form[action="/search/web/direct_search.php"] [type=submit]')
// .click('#js_x > form > button')
// .click('button[class="_42ft _4jy0 _4w98 _4jy3 _517h _51sy"]')
.wait(2000)
.screenshot('after.png')
.then(function() {
return nightmare
.wait();
})
};
};
nightmare
.useragent('...')
.use(search(string))
Upvotes: 0
Views: 487
Reputation: 1686
Nightmare's .type()
emits keyboard events. You can "press" the return key with unicode values like so: .type('selector', '\u000d')
. The example I provided below should work fine.
var Nightmare = require('nightmare');
var nightmare = Nightmare({
show: true
});
nightmare
.goto("http://www.facebook.com")
.wait('form[action="/search/web/direct_search.php"] [class=_1frb]')
.type('form[action="/search/web/direct_search.php"] [class=_1frb]', "test\u000d")
.catch(function(error) {
console.log(error)
})
Normally when searching on Facebook, the suggestions menu opens as you type a query. Pressing the search button should then search immediately search. However, if you watch Nightmare's execution, you will see the suggestions suggestions menu glitches out and closes when typing (this is electron browser's fault). So I believe you would have to press the search button twice; once to open the menu, then again to actually search.
Either way it is easier and nicer just to send a return key event.
Upvotes: 1