Javascript 确认phantom.js中的警报窗口
我有这个功能,找到按钮并点击它,但在警报出现之后,我需要使用phantom.js确认它Javascript 确认phantom.js中的警报窗口,javascript,web-scraping,phantomjs,confirm,Javascript,Web Scraping,Phantomjs,Confirm,我有这个功能,找到按钮并点击它,但在警报出现之后,我需要使用phantom.js确认它 function() { page.evaluate(function() { $('.item-list clicked').first().find($('.comment-delete')).find('a').click(); }) } 我可以模拟一下不用立即点击就可以调用警报的功能吗?或者使用函数waitFor等待此警报?(不太可能,我认为只等待DOM对象)据我所知,Phantom
function() {
page.evaluate(function() {
$('.item-list clicked').first().find($('.comment-delete')).find('a').click();
})
}
我可以模拟一下不用立即点击就可以调用警报的功能吗?或者使用函数waitFor等待此警报?(不太可能,我认为只等待DOM对象)据我所知,Phantom.JS是无头的。 您唯一能做的就是使用获取警报的上下文
window.onAlert = function(alertText){
...
}
但我想不超过这个。
您不能以编程方式关闭(一般)或呈现(在Phantom.JS中)警报
一些来源:*
*我找不到另一个帮助我回答这个问题的stackoverflow答案,但基本上你可以插入一个javascript函数来确认警报弹出: 下面是我的python webdriver实现:
def example_js_confirmation( self ):
js_confirm = 'window.confirm = function(){return true;}' # .js function to confirm a popup
self.execute_javascript( js_confirm )
self.find_by_id( 'submit' ).click()
self.execute_javascript( 'return window.confirm' ) # trigger the injected js that returns true (virtually click OK)
它在某人的待办事项清单上=):
我做到了:
// 1) override window.alert
page.evaluate(function(){
window.alert = function(msg){
window.callPhantom({alert: msg}); // will call page.onCallback()
return true; // will "close the alert"
};
});
// 2) re-bind to onAlert()
page.onCallback = function(obj) {
page.onAlert(obj.alert); // will trigger page.onAlert()
};
“确认警报”是一个令人困惑的短语,因为Javascript同时提供了alert
(显示带有OK按钮的消息)和Confirm
(显示OK和Cancel按钮),并且不清楚您指的是哪一个
至少PhantomJS 2.x的行为就像它在警报
框中单击OK,在确认
框中单击Cancel
如果要在确认框中单击确定而不是取消,您可以调用
execute\u javascript
来覆盖窗口。确认
返回true
,只要您不担心加载过程中弹出的确认
框,这些框将在execute\u javascript
有机会干预之前处理。如果你也想了解这些,我能想到的最好的办法就是修补PhantomJS源代码或通过上游代理注入JS。哦,很遗憾,但谢谢你的回答。您可能知道其他能够确认警报的工具吗?)您可以使用Zombie.js(请参阅)。还可以看看SlimerJS:它可能有窗口。。。