Python: enter file path in open file dialog window opened by another software

247 Views Asked by At

Open File Dialog to interact withI have a Python code that shall interact with a Filedialog Window opened by another software.

I need to insert Filename and Filepath in the File Dialog boxes.

The Python code identifies the window, and write correctly the file name in the file name box.

The software can also identify the item containing the address. I've use this code for this task (function List() below)

def List(): #List of all element in file dialog box


app = Application().connect(title="Open")
dlg = app.window(title="Open")


all_elements = dlg.children()


for element in all_elements:
    print(f"Class Name: {element.class_name()}")
    print(f"Control ID: {element.control_id()}")
    print(f"Control Text: {element.window_text()}")
    print("=" * 40)

In my case item is a ToolbarWindows32, Control id : 1001 The main problem is that i can't write inside it. I can solve by using pyautogui, by entering in the box with the horkey CTRL +L, but I would prefer not to emulate the keys

Here below the code I'm working on:`

from pywinauto import Application

window_name="Open"   
#window_name="Apri"  
app = Application().connect(title=window_name) #Change wit
dlg = app.window(title=window_name)

file_name="Adam2.txt"
file_path = "C:\Test"

#Set the file name in File name box
edit_control_filename = dlg.children(class_name="Edit")
edit_control_filename = edit_control_filename[0] 
edit_control_filename.type_keys(file_name)

#Set the file path in File path box
edit_controls = [control for control in dlg.children(class_name="ToolbarWindow32") if control.control_id() == 1001]
edit_control_filepath = edit_controls[0] 

edit_control_filepath.set_focus()
edit_control_filepath.click_input() 

`

1

There are 1 best solutions below

2
Vasily Ryabov On
edit_control_filename.set_edit_text(file_name)

doesn't emulate key actions. Also you can enter the full path to File name: (Nome file:) edit box at once. It should work, and you don't need to enter path separately.