Blexie
Blexie

Reputation: 11

PyWinAuto: Click() fails until mouse is physically clicked

I'm trying to get PyWinAuto to click a button as soon as it's enabled. Here's what I currently have:

while running:
        try:
            app = pywinauto.Application().connect(title='Microsoft Outlook', class_name="#32770")['Microsoft Outlook']
            app.Allow.Wait('ready', retry_interval=0.1)
            app.Allow.Click()
            print('Clicked')


        except (pywinauto.findbestmatch.MatchError, pywinauto.findwindows.ElementNotFoundError):
            time.sleep(0.1)
            pass

This works just fine if I start it running after the button is active, clicking and printing 'Clicked' as expected. If I run it before the button is active, it waits for it as expected and then seems to try and click it - Printing 'clicked' repeatedly until I click either mouse button or press enter. If I take the click() out and get it to just return app.Allow then the result is as expected regardless of when I load the script, so it does seem to be click() that's the hangup.

The behaviour is the same regardless of where I click or which window I have active - It'll work if I click anywhere or anything, but it won't do anything at all until I do... Which defeats the object of the automation, really!

Any ideas?

Thanks!

Upvotes: 1

Views: 4250

Answers (1)

Vasily Ryabov
Vasily Ryabov

Reputation: 9990

First you have to run the script as Administrator if you use .connect(...). I've already added warning about that and error in the .click() method when target process has higher privileges. See pull request #499. It will be included into coming pywinauto==0.6.5.

There is one more method: .click_input() moves mouse cursor and performs real click. While .click() just sends WM_CLICK window message (might be useful for minimized or non-active window).

P.S. By the way, for Outlook I'd recommend using Application(backend="uia") and you'll have no Win32 API specific problems. See Getting Started Guide about backends difference.

Upvotes: 1

Related Questions