2015-12-21 56 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

相关问题