Search code examples
ui-automationpywinauto

Cannot change content of an Edit control


I am using pywinauto to open a file in some software. My code is supposed to open a specific file using the Open dialog:

import pywinauto
from pywinauto.application import Application

app = Application(backend="uia").start(cmd_line="C:\\Program Files (x86)\... etc")
app.Dialog.Close.click()
app.FORAM3.Derivative.OpenSpectrum.click()
app.FORAM3.Open.Edit.SetEditText(r"C:\\Users\... etc")

The code opens the software and clicks the "Open Spectrum" button, where it gets the standard Open dialog:

Open Dialog

on the line app.FORAM3.Open.Edit.SetEditText("Paracetamol 4.foram") I get a pywinauto.findwindows.ElementNotFoundError which states that it could not find an element or a method called SetEditText.

I have already looked around on the internet and cannot find any solutions.

How to open an existing file using pywinauto from SourceForge says to use app.Open.Edit.SetEditText.

I tried using app.Open.Edit, removing the "FORAM3" part, and it could not find "Open".

I replaced this with app.Dialog.Edit and it gave me the original ElementNotFoundError.

I also looked at Open an existing excel workbook using pywinauto, however the answer to this question suggests opening the file within excel itself, which does not apply to me.

I even tried replacing SetEditText with TypeKeys and got AttributeError: Neither GUI element (wrapper) nor wrapper method 'TypeKeys' were found (typo?)

One answer in another question, "Open file from windows file dialog with python automatically", suggests to use pywinauto and gives the following code:

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()

I tried again using SetText and again got the AttributeError saying that it could not find an element or method with that name.

The accepted answer for this particular question says to use ctypes. I may resort to this if I cannot find a solution in pywinauto. The question has also been suggested as a possible duplicate of Choosing a file in Python with simple Dialog so I looked at that.

The accepted answer here suggests to use Tkinter. The other two suggest easygui and Zenity. Not what I'm after. There are no mentions of pywinauto in the other answers.

I am not asking how to open a file. From the answers I've looked at I can clearly see how to do it. My question is: Why isn't it working? It's clear that my code isn't recognising any of these Methods that have been suggested, so there must be something else wrong.

I started using Inspect.exe.

Inspect results

Part of the hierarchy has a Pane with an empty string for a name. This could be the problem, however I've worked on other software with empty panes in. In those cases I have been able to ignore the empty panes and still use the child controls. There are also three different controls with the name "Filename" which could be an issue, however since I have referred to the Edit control, it can only be one of them. I did a quick check to see whether I had to refer to the Edit control as a child of the combo box, used the line app.Dialog.combobox.Edit.SetText, and got the same AttributeError again.

My final attempt at fixing the problem was to try a different console. I have been running my code in PyCharm and found a question on jetbrains asking if it was possible to run code from PyCharm in an external console, stating that the PyCharm console did not have the same low-level control that the windows cmd.exe has.

I ran my code in the IDLE shell, and got the same error:

IDLE Shell example

I tried running the code in the regular python command prompt, and it closes with the same error. This seems enough evidence to suggest that PyCharm itself isn't the issue here.

So, to reiterate: Why doesn't python recognise any Edit control methods?


Solution

  • backend="uia" provides different hierarchy and different method names (sometimes). Default backend is "win32" so old Notepad examples are not always relevant for "uia" backend. Also old CamelCase methods are deprecated in 0.6.5 and they even exist in "win32" backend only. Use PEP-8 method names for "uia" backend like set_text. And upgrade by pip install -U pywinauto.