2016-05-30 16 views
1
var steps=[]; 
var testindex = 0; 
var loadInProgress = false;//This is set to true when a page is still loading 

var webPage = require('webpage'); 
var page = webPage.create(); 
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'; 
page.settings.javascriptEnabled = true; 
page.settings.loadImages = false;//Script is much faster with this field set to false 
phantom.cookiesEnabled = true; 
phantom.javascriptEnabled = true; 

console.log('All settings loaded, start with execution'); 
page.onConsoleMessage = function(msg) { 
    console.log(msg); 
}; 
steps = [ 

    function(){ 
     console.log('Step 1 - Open Fb home page'); 
     page.open("https://www.facebook.com/login.php", function(status){ 

     }); 
    }, 

    function(){ 
     console.log('Step 3 - Populate and submit the login form'); 
     page.evaluate(function(){ 
      document.getElementById("email").value="[email protected]"; 
      document.getElementById("pass").value="xxx"; 
      document.getElementById("login_form").submit(); 
     }); 
    }, 
    function() { 
     page.render('homepage.png'); 
    }, 
    function(){ 
     page.open("https://www.facebook.com/settings", function(status){ 

     }); 
    }, 
    function() { 
     page.render('settings.png'); 
    }, 
]; 

interval = setInterval(executeRequestsStepByStep,50); 

function executeRequestsStepByStep(){ 
    if (loadInProgress == false && typeof steps[testindex] == "function") { 
     //console.log("step " + (testindex + 1)); 
     steps[testindex](); 
     testindex++; 
    } 
    if (typeof steps[testindex] != "function") { 
     console.log("test complete!"); 
     phantom.exit(); 
    } 
} 


page.onLoadStarted = function() { 
    loadInProgress = true; 
    console.log('Loading started'); 
}; 
page.onLoadFinished = function() { 
    loadInProgress = false; 
    console.log('Loading finished'); 
}; 
page.onConsoleMessage = function(msg) { 
    console.log(msg); 
}; 

如何注入 「https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js」,並使用如何在Phantomjs中注入jquery(js)?

$('#email').attr("value", "[email protected]"); 

$('#pass').attr("value", "xxxx"); 

$('#login_form').submit(); 

我嘗試所有方法,但劇本着工作。

+0

我沒有看到你試圖在代碼中使用jQuery的地方。請[編輯]您的問題以顯示實際的代碼。你怎麼知道它不起作用?你是否等了一段時間?你有截圖嗎? –

回答

1

你可以在你的頁面對象上調用includeJs來做到這一點,並在注入後將你的評估函數放在回調函數內部,並使用你想要執行的任何JQuery代碼。

此外,您將需要在代碼開始時刪除用戶代理。它的存在可以防止加載外部JQuery JavaScript。你還需要增加你傳遞給setInterval的時間。 50毫秒是沒有足夠的時間加載JQuery,操縱DOM和提交表單。在10秒內聽起來更安全。

function(){ 
    console.log('Step 3 - Populate and submit the login form'); 
    page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js', function() { 
     page.evaluate(function(){ 
     $('#email').attr("value", "[email protected]"); 
     $('#pass').attr("value", "xxxx"); 
     $('#login_form').submit(); 
     }); 
    }); 



interval = setInterval(executeRequestsStepByStep, 10000); 
+0

如果喜歡它100%的工作,我知道。你可以編輯我的代碼嗎? –

+0

我剛剛重新編輯答案。我運行你的代碼,並在修復上述所有的測試之後進行測試..它的工作原理和能夠操縱DOM字段並提交。當然,在提交後,Facebook會阻止您繼續進行,因爲Cookie被禁用。但是,爲了您的問題的目的,這應該解決它。當你的結局很好時,請修改並接受答案:)乾杯! –