Serinkan
Serinkan

Reputation: 53

How to move a firefox window with pywinauto

Here is my code :

The commented part is what I tried earlier, but didn't work.

    try:
        # app = Application(backend="uia").start("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
        # time.sleep(5)
        # mozilla = app.window_(title_re = ".*Mozilla Firefox")
        # time.sleep(5)
        # mozilla.move_window(200, 200, 200, 200, True)
        app = Application().start("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
        dlg_spec = app.window()
        dlg_spec.move_window(x=None, y=None, width=200, height=100, repaint=True)
    except AppStartError:
        print("CANNOT START !!")
        return False
    except ElementNotFoundError : 
        print("COULD NOT FOUND THE WINDOW !!")
        return False
    except ElementAmbiguousError :
        print("TOO MANY FIREFOX !!")
        return False

I get COULD NOT FOUND THE WINDOW !! And in the commented one I get this error :

    mozilla.move_window(200, 200, 200, 200, True)
  File "C:\Users\XX\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pywinauto\application.py", line 180, in __call__
    raise AttributeError("Neither GUI element (wrapper) " \
AttributeError: Neither GUI element (wrapper) nor wrapper method 'move_window' were found (typo?)

I would appreciate any help, thanks.

Upvotes: 0

Views: 2963

Answers (1)

Vasily Ryabov
Vasily Ryabov

Reputation: 9991

You have to use proper title or best_match value inside .window(...) call. So that pywinauto knows which window to search. For example, app.window(title_re=".*Firefox").

To list all the titles use this statement:

print([w.window_text() for w in app.windows()])

Also .draw_outline() is useful method to highlight found element.

Recommended reading: Getting Started Guide


EDIT:

If .windows() returns an empty list, you have to re-connect by title using method connect instead of / in addition to start. We have a feature request to detect spawned processes, but it's of low priority for now.

EDIT2:

How it works for me:

from pywinauto import Application

app = Application(backend="win32").start("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
app = Application(backend="win32").connect(title='Mozilla Firefox', found_index=0, timeout=5)
dlg_spec = app.window(title='Mozilla Firefox', found_index=0)
dlg_spec.move_window(x=None, y=None, width=200, height=100, repaint=True)

Upvotes: 1

Related Questions