2016-11-20 42 views
0

儘管.type()效果很好,但我不能讓nightmareJS點擊搜索按鈕。 可能是什麼問題?NightmareJS .click()方法 - 選擇器問題

.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"]') 

開發工具控制檯找到按鈕:

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>​ 

後來編輯:

你去那裏,我的朋友,我已經去除了進口,把一切都在一個單一的文件,我已經用cookie登錄,所以這是複製問題的完整代碼:

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)) 
+0

歡迎來到Stack Overflow!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –

+0

@JoeC我相應地更新了我的帖子。 – Dobi

+0

我打算假設你沒有閱讀過關於如何調試的文章。 –

回答

1

Nightma re的.type()發出鍵盤事件。你可以用「unicode」值「按下」返回鍵,例如:.type('selector', '\u000d')。我在下面提供的示例應該可以正常工作。

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) 
    }) 

通常情況下,在Facebook上搜索時,建議菜單會在您輸入查詢時打開。然後按下搜索按鈕應立即搜索搜索。但是,如果您觀看Nightmare的執行,您將看到建議建議菜單故障並在打字時關閉(這是電子瀏覽器的錯誤)。所以我相信你將不得不按兩次搜索按鈕;一旦打開菜單,然後再次實際搜索。

無論哪種方式,它只是發送一個返回鍵事件更容易和更好。