Search code examples
pythonweb-scrapingautomationpywinauto

How to type in the file explorer using PyWinAuto


I am trying to write a web scraper in python that uploads a file to a website (through a dashboard). To upload a file, I need to click on this box.

File upload box

When I click on this box it opens a File Explorer window. I want to be able to type into the 'File Name:' box.

Windows file explorer I assume it's possible with PyWinAuto but I have never used it before and have no idea how I'm supposed to go about it. The furthest I've got is connecting to the window

app = Application(backend="uia").connect(path="explorer.exe", title="Open")

Solution

  • I managed to figure it out in the end.

    Instead of connecting to 'explorer.exe', I had to connect to the browser window that contained the file explorer box using this code.

    app = Application(backend='uia').connect(title='Browser window name',timeout=100)
    
    fileBox = app.BrowserWindowName.child_window(title="File name:", auto_id="1148", control_type="Edit").wrapper_object()
    

    This targeted the 'File Name:' box that I was then able to send a file path to by using the type_keys() method

    fileBox.type_keys('pathToFile', with_spaces=True)