3

I'm doing automation scripting in Python for my desktop application. In that I'm sending TAB key/any key to my windows form. But I'm not able to find handle of that Windows form in my Python script.

Here is the sample code snippet :

__author__ = 'juhis'

import SendKeys
import sys
import os
from Tkinter import *
import ctypes
import win32gui
import pywinauto

pwapp = pywinauto.application.Application()
whandle = pywinauto.findwindows.find_windows(title_re='Form1',class_name='WindowsForms10.Window.8.app.0.2bf8098_r13_ad1')[0]
window1 = pwapp.window_(handle=whandle)
window1.SetFocus()

SendKeys.SendKeys("""{PAUSE 2}""")
SendKeys.SendKeys("""{TAB 2}{PAUSE 2}{ENTER}""")

Please help me to figure out the issue.

-Thanks

3
  • What is the output of the script? Commented Jan 13, 2016 at 12:33
  • i'm just getting following output on console : with exit code 1 But those TAB buttons not getting clicked on that Win Form Commented Jan 13, 2016 at 12:52
  • It looks like you use old version of SWAPY for code generation. Please use the latest SWAPY and pywinauto 0.5.4. If the problem persists, just update the question and let me know in the comments. Commented Jan 13, 2016 at 13:05

2 Answers 2

5

The code can be re-written simpler:

import pywinauto

app = pywinauto.application.Application().connect(title_re='Form1')
Form1 = app.Window_(title_re='Form1', class_name='WindowsForms10.Window.8.app.0.2bf8098_r13_ad1')
Form1.SetFocus()
Form1.TypeKeys("{PAUSE 2}")
Form1.TypeKeys("{TAB 2}{PAUSE 2}{ENTER}")

TypeKeys automatically sets a focus to the Form1 and types the keys. SendKeys doesn't set a focus because it's not aware about the window. That's probably why it doesn't work with SendKeys.

[EDIT] Of course you need to run the script as Administrator.

Sign up to request clarification or add additional context in comments.

3 Comments

Vasily, I tried your code. But still its not working. Its set the focus on that form, it means its getting a proper handle of that form. But its not sending any keys to that form. And I'm getting output message on console "return with exit code 0"
Are you running your Python script as Administrator?
The comment is very useful. Running as Admin fixed my problem as well. Vasily, thanks.
0

I got a fixed for this issue. The mistake I was doing is that, I was not running my script as Administrator. So that's why SendKeys event were not happening.

But when I ran my script as Administrator, SendKeys event successfully sent to Windows form.

Thanks Vasily for your help.

4 Comments

You're welcome! Just for your info: marking correct answers as accepted is considered polite on StackExchange. It's a signal for other users that the problem is really solved. And if you don't do that, it can demotivate experts to answer your questions.
Sure Vasily. I'll keep this thing in mind. But for now, I dont have enough reputation points to mark correct answer.
You need 15 rep points to vote up, but accepting answers is available to every author of a question.
ohoo yeah !! got it. I was confused in that. thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.