2015-12-21 57 views
2

當我將鼠標懸停在網頁的圖片上時,它會在狀態欄javascript:main.somefunction("arg1","arg2","arg3")中顯示JavaScript功能。是否有可能在網頁上下文中運行該功能並使用PhantomJS呈現結果?我可以在PhantomJS中運行網頁的JavaScript功能嗎?

+0

您使用哪個PhantomJS版本?請註冊onConsoleMessage,onError,onResourceError,onResourceTimeout事件([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file-1_phantomerrors-js))。也許有錯誤。 –

回答

0

如果你有一個元素如

<a href='javascript:main.somefunction("arg1","arg2","arg3")'> 
    <img src='someImage.jpg'> 
</a> 

,那麼你應該看到在狀態欄中的鏈接的URI。由於頁面必須知道main對象才能調用該對象,因此main必須是全局範圍內的對象(在window對象上)。您可以在頁面上下文中撥打main.somefunction("arg1","arg2","arg3")window.main.somefunction("arg1","arg2","arg3")

如果函數異步更改頁面,則必須等待更改。靜態延遲可以用setTimeout()完成。如果您需要等待特定條件,則可以使用waitFor() function from the examples directory

page.open(url, function(status){ 
    if (status === "fail") { 
     console.log("Load failed"); 
     phantom.exit(1); 
     return; 
    } 

    page.evaluate(function(){ 
     main.somefunction("arg1","arg2","arg3") 
    }); 

    setTimeout(function(){ 
     page.render("screenshot.png"); 
     phantom.exit(); 
    }, 3000); // 3 seconds delay 
}); 
+1

現在我得到一個錯誤TypeError:undefined不是一個函數(評估'main.somefunction(「arg1」,「arg2」,「arg3」)') – user5704481

相關問題