2016-07-16 80 views
1

我想用CasperJS調用javascript方法。 該網頁只是一個鏈接,允許我改回默認的國家。CasperJS在瀏覽器控制檯上調用JavaScript函數

<a id="defaultCountry" href="javascript:__doPostBack('cty$UK$default','')">Default Country</a> 

我想要CasperJS調用單擊鏈接後調用的javascript方法。 我認爲模仿鼠標點擊鏈接將調用JavaScript方法,但它沒有。 我試過沒有成功以下方法:

casper.then(function() { 
     casper.click(x('//*[@id="defaultCountry"]')); 
     casper.evaluate(function() { 
       __doPostBack('cty$UK$default',''); //this is the javascript function. im not sure if thats how you would call it though 
     }); 

this.clickLabel('Default Country', 'a'); 

我知道,如果我調用瀏覽器控制檯上的JavaScript函數,它會工作。我簡單地輸入:

__doPostBack('cty$UK$default',''); 

在控制檯,它神奇的作品。 任何幫助表示讚賞!


編輯:

@Rippo這是我所遇到的一個片段。 CasperJS似乎繞過了我的評估聲明。最後幾行直接來自控制檯。由於屏幕截圖,我知道我的頁面會加載選擇器。我甚至用casper.waitforselector方法來確認。

casper.thenOpen('http://example.com'); 

casper.wait(5000, function() { 
    console.log('page opened'); 
    casper.capture('page.png'); 
    console.log('capture page complete'); 
}); 

casper.thenEvaluate(function() { 
    console.log('invoking javascript'); 
    __doPostBack('cty$UK$default',''); 
    console.log('javascript invoked'); 
}); 

這是從控制檯:

[info] [phantom] wait() finished waiting for 5000ms. 
page opened 
[debug] [phantom] Capturing page to C:/Users/page.png 
[info] [phantom] Capture saved to C:/Users/page.png 
capture page complete 
[info] [phantom] Step _step 8/8 http://example.com (HTTP 200) 
[info] [phantom] Step _step 8/8: done in 16240ms. 
[info] [phantom] Done 8 steps in 16259ms 
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true 
[debug] [phantom] url changed to "about:blank" 

這是在退出前控制檯的最後一行。


編輯2016年7月17日上午12點40

@Artjom B. 這是我用你的建議跑了代碼片段:

...snipped some prior  
function() { 
    console.log('Page loaded'); 
    casper.capture('page.png'); 
    console.log('Starting 1st postback call'); 
    this.evaluate(function() { 
     console.log('postback call'); 
     __doPostBack('cty$UK$default',''); 
     }); 
    console.log('passed postback'); 
    casper.capture('post-postback.png'); 

//At this point, it skips this function and goes straight to console.log then comes back to this function. Not sure why... 
casper.then(function() { 
    console.log('trying to change again'); 
    this.click(x('//*[@id="defaultCountry"]')); 
    this.evaluate(function() { 
     __doPostBack('cty$UK$default',''); 
     console.log('javascript invoked'); 
    }); 
}); 

//skipped to this console.log 
console.log('waiting country to change'); 
this.waitForSelector('.countryuk', 
    function() { 
     console.log('country change completed. Capturing image'); 
     this.capture('uk.png'); 
    }, 

    function() { 
     console.log('timed out waiting for country to change.'); 
     this.capture('uk-timeout.png'); 
    },5000); 

};

這裏是控制檯輸出:

Page loaded 
[debug] [phantom] Capturing page to C:/Users/page.png 
[info] [phantom] Capture saved to C:/Users/page.png 
Starting 1st postback call 
Console: postback call 
Error: ReferenceError: Can't find variable: __doPostBack 
passed postback 
[debug] [phantom] Capturing page to C:/Users/post-postback.png 
[info] [phantom] Capture saved to C:/Users/post-postback.png 
waiting country to change 
[info] [phantom] Step anonymous 10/11 http://example.com/page.aspx?r=2 (HTTP 200) 
trying to change again 
[debug] [phantom] Mouse event 'mousedown' on selector: xpath selector: //*[@id="defaultCountry"] 
[debug] [phantom] Mouse event 'mouseup' on selector: xpath selector: //*[@id="defaultCountry"] 
[debug] [phantom] Mouse event 'click' on selector: xpath selector: //*[@id="defaultCountry"] 
Error: ReferenceError: Can't find variable: __doPostBack 
Error: ReferenceError: Can't find variable: __doPostBack 
[info] [phantom] Step anonymous 10/11: done in 22567ms. 
[info] [phantom] Step _step 11/11 http://example.com/page.aspx?r=2 (HTTP 200) 
[info] [phantom] Step _step 11/11: done in 22573ms. 
[warning] [phantom] Casper.waitFor() timeout 
timed out waiting for country to change. 
[debug] [phantom] Capturing page to C:/Users/uk-timeout.png 
[info] [phantom] Capture saved to C:/Users/uk-timeout.png 
[info] [phantom] Done 11 steps in 27825ms 
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true 
[debug] [phantom] url changed to "about:blank" 

顯然,這無法找到回傳功能。我不知道爲什麼。這不是一個隱藏的元素。這正如我在上面發佈的(一個鏈接)。它嵌套在一堆div標籤中,但這就是它。謝謝你的幫助!

+0

我已經看到了類似的問題與回發調用我的問題是,Casperjs犯規過程我的eval函數,我不知道爲什麼。 https://開頭計算器。com/questions/25107976/can not-navigate-with-casperjs-evaluate-and-dopostback-function https://stackoverflow.com/questions/17830597/casperjs-how-to-call-dopostback – kubermeso

+0

歡迎來到Stack Overflow!您使用哪個PhantomJS版本?請註冊到'resource.error','page.error','remote.message'和'casper.page.onResourceTimeout'事件([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file -2_caspererrors-JS))。也許有錯誤。 –

+0

你還沒有回答我的第一個問題。無論如何,你必須找出加載'__doPostBack'函數的請求沒有被加載的原因。這是一個HTTPS請求嗎? –

回答

0

您可以訪問該鏈接的href並使用eval函數。

var str = document.getElementById("defaultCountry").href; 
eval(str.substring(str.indexOf(":") + 1)); 
+0

一旦我解散了,CasperJS就爆發了。我把你的代碼之前和之後的console.log。它會先解釋console.log,但不會在之後解釋。 – kubermeso

+0

@kubermeso你是什麼意思「爆發」?你在哪裏做宣言和eval電話? – afuous

+0

請參閱我上面的修改。我把你的陳述放在casper.thenEvaluate函數中。我也用casper.wait進行了試用。工作和casperjs都無法繞過代碼 – kubermeso

1

怎麼樣只(除去casper.thencasper.click

casper.thenEvaluate(function() { 
     __doPostBack('cty$UK$default',''); 
}); 
+0

作爲旁註'console.log('invoking javascript');'用echo評估web瀏覽器控制檯,而不是從其運行的命令控制檯。我不知道JS是否已經完成加載?我懷疑你在頁面某處有一個JS錯誤。請參閱@artjom評論 – Rippo

相關問題