Which is the best way to interact with already open native OS dialog boxes like (Save AS) using Python? Which is the best way to interact with already open native OS dialog boxes like (Save AS) using Python? selenium selenium

Which is the best way to interact with already open native OS dialog boxes like (Save AS) using Python?


While looking for a possible solution for this I came across several solutions on SO and otherwise.Some of them were using AutoIT, or editing browsers profile to make it store the file directly without a prompt.

I found all this solution too specific like you can overcome the issue for Save As dialog by editing the browser profile but if later you need to handle some other window then you are stuck.For using AutoIT is overkill, this directly collides for the fact I choose Python to do this task. (I mean Python is itself so powerful, depending on some other tool is strict NO NO for any Pythonist)

So after a long search for a possible generic solution to this problem which not only serves any one who is looking to handle any Native OS dialog boxes like 'Save As', 'File Upload' etc in the process of automating a web application using selenium web driver but also to any one who wants to interact with a specific window using only Python APIs.

This solution makes use of Win32gui, SendKeys modules of Python.I will explain first a generic method to get hold of any window desired then a small code addition which will also make this usable while automating a web application using Selenium Webdriver.

Generic Solution::

import win32guiimport reimport SendKeysclass WindowFinder:"""Class to find and make focus on a particular Native OS dialog/Window """    def __init__ (self):        self._handle = None    def find_window(self, class_name, window_name = None):    """Pass a window class name & window name directly if known to get the window """        self._handle = win32gui.FindWindow(class_name, window_name)    def _window_enum_callback(self, hwnd, wildcard):    '''Call back func which checks each open window and matches the name of window using reg ex'''        if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) != None:            self._handle = hwnd    def find_window_wildcard(self, wildcard):""" This function takes a string as input and calls EnumWindows to enumerate through all open windows """    self._handle = None        win32gui.EnumWindows(self._window_enum_callback, wildcard)    def set_foreground(self):    """Get the focus on the desired open window"""        win32gui.SetForegroundWindow(self._handle)win = WindowFinder()win.find_window_wildcard(".*Save As.*") win.set_foreground()path = "D:\\File.txt"            #Path of the file you want to Save ent = "{ENTER}"                  #Enter key stroke.SendKeys.SendKeys(path)          #Use SendKeys to send path string to Save As dialogSendKeys.SendKeys(ent)           #Use SendKeys to send ENTER key stroke to Save As dialog

To use this code you need to provide a string which is the name of the window you want to get which in this case is 'Save As'. So similarly you can provide any name and get that window focused.Once you have the focus of the desired window then you can use SendKeys module to send key strokes to the window which in this case includes sending file path where you want to save the file and ENTER.

Specific to Selenium Webdriver::

The above specified code segment can be used to handle native OS dialog boxes which are triggered through a web application during the automation using Selenium Webdriver with the addition of little bit of code.

The issue you will face which I faced while using this code is that once your automation code clicks on any Web Element which triggers a native OS dialog window, the control will stuck at that point waiting for any action on the native OS dialog window. So basically you are stuck at this point.

The work around is to generate a new thread using Python threading module and use it to click on the Web Element to trigger the native OS dialog box and your parent thread will be moving on normally to find the window using the code I showed above.

#Assume that at this point you are on the page where you need to click on a Web Element to trigger native OS window/dialog boxdef _action_on_trigger_element(_element):    _element.click()trigger_element = driver.find_element_by_id('ID of the Web Element which triggers the window')th = threading.Thread(target = _action_on_trigger_element, args = [trigger_element])  #Thread is created here to call private func to click on Save buttonth.start()                          #Thread starts execution heretime.sleep(1)                       #Simple Thread Synchronization handle this case.          #Call WindowFinder Classwin = WindowFinder()win.find_window_wildcard(".*Save As.*") win.set_foreground()path = "D:\\File.txt"            #Path of the file you want to Save ent = "{ENTER}"                  #Enter key stroke.SendKeys.SendKeys(path)          #Use SendKeys to send path string to Save As dialogSendKeys.SendKeys(ent)           #Use SendKeys to send ENTER key stroke to Save As dialog#At this point the native OS window is interacted with and closed after passing a key stroke ENTER.# Go forward with what ever your Automation code is doing after this point

NOTE::

When using the above code in automating a web application, check the name of the window you want to find and pass that to find_window_wildcard(). The name of windows are browser dependent. E.g. A window which is triggered when you click on an Element to Upload a file is called 'File Upload' in Firefox and Open in Chrome.Uses Python2.7

I hope this will help any one who is looking for a similar solution whether to use it in any generic form or in automating a web application.

EDIT:

If you are trying to run your code through command line arguments then try to use the thread to find the window using Win32gui and use the original program thread to click on the element (which is clicked here using the thread). The reason being the urllib library will throw an error while creating a new connection using the thread.)

References::

SO Question

SenKeys

Win32gui


There is a Python module called win32ui. Its found in the Python for Windows extensions package. You want the CreateFileDialog function.

Documentation

Edit:This is a save dialog example. Check the documentation for the other settings.

import win32uiif __name__ == "__main__":    select_dlg = win32ui.CreateFileDialog(0, ".txt", "default_name", 0, "TXT Files (*.txt)|*.txt|All Files (*.*)|*.*|")    select_dlg.DoModal()    selected_file = select_dlg.GetPathName()    print selected_file