在phantom.js中确认警报窗口

Ali*_*sky 7 javascript confirm web-scraping phantomjs

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

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

也许我可以模拟立即点击提醒的功能?或者使用函数waitFor来等待这个警告?(不太可能这样,waitFor只等待DOM对象我认为如此)

小智 6

我找不到另一个帮助我回答这个问题的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)
Run Code Online (Sandbox Code Playgroud)

这是某人的待办事项列表=): Selenium Desired Capabilities - 为PhantomJS驱动程序设置handleAlerts


Mar*_*coL 3

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

window.onAlert = function(alertText){
    ...
   }
Run Code Online (Sandbox Code Playgroud)

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

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