Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file from windows file dialog with python automatically

I do automated testing and get a file dialog. I want to choose a file from the windows open file dialog with python or selenium.

NOTE: The dialog is given by an other program. I don't want to create it with Tkinter.

The Window looks like:

THIS.

How to do this?

like image 388
kame Avatar asked May 04 '16 12:05

kame


People also ask

How do you display a file dialog for opening a file in Python?

Use the askopenfilename() function to display an open file dialog that allows users to select one file. Use the askopenfilenames() function to display an open file dialog that allows users to select multiple files.

What is file dialog Python?

Build A Paint Program With TKinter and Pythonfiledialog is one of the tkinter modules that provides classes and library functions to create file/directory selection windows. You can use filedialog where you need to ask the user to browse a file or a directory from the system.


2 Answers

Consider using the pywinauto package. It has a very natural syntax to automate any GUI programs.

enter image description here

Code example, opening a file in notepad. Note that the syntax is locale dependent (it uses the visible window titles / control labels in your GUI program):

from pywinauto import application
app = application.Application().start_('notepad.exe')
app.Notepad.MenuSelect('File->Open')
# app.[window title].[control name]...
app.Open.Edit.SetText('filename.txt')
app.Open.Open.Click()
like image 177
Rob Avatar answered Sep 22 '22 17:09

Rob


You can use ctypes library.

Consider this code:

import ctypes

EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
SendMessage = ctypes.windll.user32.SendMessageW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible

def foreach_window(hwnd, lParam):
    if IsWindowVisible(hwnd):
        length = GetWindowTextLength(hwnd)
        buff = ctypes.create_unicode_buffer(length + 1)
        GetWindowText(hwnd, buff, length + 1)

        if(buff.value == "Choose File to Upload"): #This is the window label
            SendMessage(hwnd, 0x0100, 0x09, 0x00000001 )
    return True

EnumWindows(EnumWindowsProc(foreach_window), 0)

You loop on every open window, and you send a key stroke to the one you choose.

The SendMessage function gets 4 params: the window hendler (hwnd), The phisical key to send - WM_KEYDOWN (0x0100), The virtual-key code of tab (0x09) and the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag in the 4th argument.

You can also send key up, key down, chars, returns and etc... Use the documentation for help.

I used this as a reference: Win32 Python: Getting all window titles

Good luck!

like image 23
Gal Silberman Avatar answered Sep 20 '22 17:09

Gal Silberman