[Wtr-general] How do I click the "OK" button on a java alert?

Nathan forum-watir-users at openqa.org
Wed Jan 3 11:03:38 EST 2007


Hey, so it seems like you're getting some mixed ideas here.  First thing, WATIR uses AutoIt to click on JavaScript or Modal dialogs from IE.  Second AutoIt isn't flakey - it's very solid, but you need to know how to use it.  I have a solution to clicking on modal dialogs, and I just run it when I think there could be a dialog.  Here's my method, and I'll explain it in detail.

***********************************************************
def intCloseDialog()
  # Create an AutoIt object to use to click on open dialogs
  objAutoit = WIN32OLE.new("AutoItX3.Control")

  # Look for any open dialogs and close them
  ans1 = objAutoit.ControlClick("Microsoft Internet Explorer",'', 'OK')
  ans2 = objAutoit.ControlClick("Security Information",'', '&Yes')
  ans3 = objAutoit.ControlClick("Security Alert",'', '&Yes')

  # Release the clicker object
  objAutoit = nil

  return (ans1 + ans2 + ans3)
end # End: def voidCloseDialog()
***********************************************************

First off, you'll need to require "win32ole" for this to work.  If you have required watir already then, win32ole should also already be required for you.  Now on the first line I create a new WIN32OLE object that is associated with the AutoItX3.Control OLE server. On the second line I tell the objAutoit object to click on a control (dialog in this case) with the title Microsoft Internet Explorer and on the button labeled "OK".  I do a similar thing on the next two lines, those are just other possible titles for my dialogs.  On the fifth line I set my object to nil so that the memory is cleaned up, then I return the result of having clicked on each on the dialogs.  What happens here is AutoIt looks for the dialog first and if it exists then it tries to click on the button you told it to, also if it exists then it returns 1.  But if it doesn't exist, then ControlClick returns zero.  I just use that number for debugging - it is in no way required for anything.

I hope that this actual answer helps you out a little more.  AutoIt is a very good tool - I use it in another program I've installed called AutoHotkey.  It is a macro program for Windows.  AutoIt has a lot of capabilities, so don't not use it! :)

Nathan
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=5802&messageID=16752#16752


More information about the Wtr-general mailing list