asguldbrandsen
asguldbrandsen

Reputation: 173

Error when trying to send text to application with Pywinauto: AttributeError

I am using Pywinauto to automate some interaction steps with an application that opens during a browser login session.

Lets call the application program.exe. It is actually a Chrome Extension that opens and prompts for a password.

import pywinauto as pwa
from pywinauto import application
from pywinauto import keyboard

app = application.Application()
app = app.Connect(path=r"C:\path\program.exe")                 
win.Part.Click() #not completely sure why i do this
app['Insert password']['Edit'].send('password')

It seems that I am able to connect to the program, but when I try to send text to the program I get an error. When i run the above this error occurs:

AttributeError: Neither GUI element (wrapper) nor wrapper method 'send' were found (typo?)

If i replace this:

app['Insert password']['Edit'].send('password')

With this:

app['Insert password'].SendKeys.send('password')

I get this error:

MatchError: Could not find 'SendKeys' in 'dict_keys(['Insert password for MyName:Static', 'Static', 'Insert password for MyName:Edit', 'Edit', 'OK', 'OKButton', 'Button', 'Button0', 'Button1', 'Button2', 'Cancel', 'CancelButton', 'Insert password for MyName:Static0', 'Insert password for MyName:Static1', 'Insert password for MyName:Static2', 'Insert password for MyName:', 'Static0', 'Static1', 'Static2'])'

Upvotes: 0

Views: 2169

Answers (1)

Vasily Ryabov
Vasily Ryabov

Reputation: 9991

  • There is no method send for any of the controls. SendKeys is not a method, but a function inside module keyboard so the correct usage is keyboard.SendKeys('password').

  • But method .type_keys('password') puts the control into focus and then does the same as keyboard.SendKeys. You may need to use with_spaces=True if password contains spaces. Special symbols like % must be escaped so: {%}. This method is powerful because it supports hot key combinations with Alt, Shift, Ctrl etc. See the docs about keyboard module. Usage for your case:

    app['Insert password']['Edit'].type_keys('password', with_spaces=True)


  • Method .set_edit_text('password') can be even more useful: it doesn't type keys char by char, but sends the whole raw text to the control (no support for special keys, just text). This method doesn't require the control to be in focus.

Upvotes: 1

Related Questions