2017-07-15 156 views
0

我希望能夠使用POST請求運行Web自動化腳本文件。下面是一個代碼示例其中,例如,我想一個參數傳遞給該文件運行與用戶指定的URL,而不是所示的.goto():通過POST請求將參數傳遞給JavaScript文件

var Nightmare = require('nightmare'); 
var nightmare = Nightmare({ show: true }); 

nightmare 
    .goto('https://duckduckgo.com') 
    .type('#search_form_input_homepage', 'github nightmare') 
    .click('#search_button_homepage') 
    .wait('#zero_click_wrapper .c-info__title a') 
    .evaluate(function() { 
    return document.querySelector('#zero_click_wrapper .c-info__title a').href; 
    }) 
    .end() 
    .then(function (result) { 
    console.log(result); 
    }) 
    .catch(function (error) { 
    console.error('Search failed:', error); 
    }); 

有什麼辦法來傳遞參數像這樣直接導入.js文件?

+0

總結這是接受一個參數並通過URL函數調用的函數。 –

回答

1

裹在接受參數的函數調用nightmare

var Nightmare = require('nightmare'); 
 
var nightmare = Nightmare({ show: true }); 
 

 
function nightmareWrapper(urlArgument) { 
 
    nightmare 
 
     .goto(urlArgument) 
 
     .type('#search_form_input_homepage', 'github nightmare') 
 
     .click('#search_button_homepage') 
 
     .wait('#zero_click_wrapper .c-info__title a') 
 
     .evaluate(function() { 
 
      return document.querySelector('#zero_click_wrapper .c-info__title a').href; 
 
     }) 
 
     .end() 
 
     .then(function (result) { 
 
      console.log(result); 
 
     }) 
 
     .catch(function (error) { 
 
      console.error('Search failed:', error); 
 
     }); 
 
} 
 

 
// And call it like so: 
 

 
var urlArgument = 'https://duckduckgo.com'; 
 
nightmareWrapper(urlArgument);

+0

現在全部點擊了,謝謝! –