Python / IE7 - how to bypass alerts

I have automation scripts written in Python for IE7. It works great except when an alert box is triggered. The alert box is triggered in the onload for the body, which means it gets executed before my script gets to it.

I have the following wait function that just hangs until the alert button is clicked:

def _waitReady(self):
    """ Waits until IE finishes loading. """
    while self.oIE.Busy: time.sleep(0.1)
    while self.oIE.Document.readyState != "complete": time.sleep(0.1)

      

I would like to just turn off all alert boxes, but IE seems to be unable to do so. Alternatively, I was hoping to detect an alert window open and then just take action in response (for example, just clicking on it). Are there any suggestions on how to do this?

EDIT: I know I can iterate over IE child windows, and I know I can close the alert box if I find it.

win32gui.EnumChildWindows(hwnd, function, None) # cycles through child windows
win32api.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) # closes window

      

Now I just need to somehow identify the window as an alert window. Thoughts?

+3


source to share


2 answers


Solved my problem. I ended up just iterating through every window on the system, checking for the class name and closing the window if it was an alert class (# 32770).

I tried a more targeted approach, but I couldn't find the alert box in the IE application.



def _waitReady(self):
while self.oIE.Busy:
    time.sleep(0.1)
    waitCount += 0.1

    if waitCount > 5:
        print("killing alert boxes")
        _killAllAlertBoxes()
       waitCount = 0

def _killAllAlertBoxes():
# iterates through all active windows
win32gui.EnumChildWindows(0, _closeWindow, None)

def _closeWindow(hwnd, lparam):
# if window is an alert box (class = '#32770')
if win32gui.GetClassName(hwnd) == '#32770':
    print("Closing Alert Box")
    win32api.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) # closes window

      

Thanks to those who answered!

+1


source


This code works for warning windows in firefox, you may need to change it for IE.



   import time

   time.sleep(2)
   driver.switch_to_alert().accept(); time.sleep(2); time.sleep(2); time.sleep(2);time.sleep(2)
   driver.switch_to_window(browser.window_handles[1])

      

0


source







All Articles