javascript 在 phantom.js 中确认警报窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19903146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:11:45  来源:igfitidea点击:

confirm alert window in phantom.js

javascriptweb-scrapingphantomjsconfirm

提问by Alice Polansky

i have this function which find button and click it, but after that alert appears and i need to confirm it using phantom.js

我有这个功能,可以找到按钮并单击它,但是在出现警报后,我需要使用 phantom.js 进行确认

function() {
  page.evaluate(function() {
    $('.item-list clicked').first().find($('.comment-delete')).find('a').click();
  })
}

may be i can emulate function which call alert whithout click immediately ? or use function waitFor for waiting this alert?(unlikely this, waitFor waiting only for DOM objects i think so)

也许我可以模拟无需立即点击即可调用警报的功能?或使用函数 waitFor 等待此警报?(不太可能,waitFor 仅等待 DOM 对象,我认为是这样)

采纳答案by MarcoL

As far as I know, Phantom.JS is headless. The only thing you can do is get the context of the alert with

据我所知,Phantom.JS 是无头的。您唯一能做的就是获取警报的上下文

window.onAlert = function(alertText){
    ...
   }

but no more than this I think. You cannot either close (in general) or render (in Phantom.JS) the alert programmatically.

但我认为仅此而已。您不能以编程方式关闭(通常)或呈现(在 Phantom.JS 中)警报。

Some sources:
* Render JavaScript alerts with Phantom.JS
* Close alert programmatically

一些来源:
*使用 Phantom.JS 呈现 JavaScript 警报
* 以编程方式关闭警报

回答by foupfeiffer

I can't find the other stackoverflow answer that helped me answer this as well but basically you can inject a javascript function that will confirm the alert popup:

我找不到帮助我回答这个问题的其他 stackoverflow 答案,但基本上你可以注入一个 javascript 函数来确认警报弹出窗口:

Here's my python webdriver implementation:

这是我的 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)

It's on somebody's todo list =) : Selenium Desired Capabilities - set handlesAlerts for PhantomJS driver

它在某人的待办事项列表中 =) : Selenium Desired Capabilities - 为 PhantomJS 驱动程序设置 handlesAlerts

回答by Silas S. Brown

"Confirm an alert" is a confusing phrase, because Javascript provides both alert(which shows a message with an OK button) and confirm(which shows OK and Cancel buttons), and it's not clear which one you mean.

“确认警报”是一个令人困惑的短语,因为 Javascript 提供alert(显示带有 OK 按钮的消息)和confirm(显示 OK 和 Cancel 按钮),并且不清楚您指的是哪个。

At least PhantomJS 2.x behaves as if it clicks OK on alertboxes and Cancel on confirmboxes.

至少 PhantomJS 2.x 的行为就像在alert框上单击 OK和在confirm框上单击 Cancel 一样。

If you want to click OK instead of Cancel on confirmboxes, you could call execute_javascriptto override window.confirmto return trueas long as you're not worried about confirmboxes that pop up during the load processwhich will be dealt with before your execute_javascripthas a chance to intervene. If you want to catch those as well, the best thing I can think of is to patch PhantomJS source or inject the JS via an upstream proxy.

如果您想在confirm框上单击 OK 而不是 Cancel ,您可以调用execute_javascriptoverridewindow.confirm返回true,只要您不担心加载过程confirm中弹出的框将在您有机会干预之前处理。如果你也想抓住那些,我能想到的最好的办法是修补 PhantomJS 源或通过上游代理注入 JS。execute_javascript

回答by Chornsby

Using splinterin Python you would want to add these lines (from the docs).

在 Python 中使用splinter您会想要添加这些行(来自docs)。

alert = browser.get_alert()
alert.accept()

回答by Adrien Joly

I did that:

我是这样做的:

// 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()
};