Search code examples
pythonautomationpyautogui

How to add an application to a list with PyWinAuto?


I'm trying to automate an application on Python that deal with browser window.
I need to add applications to a list after I open then, so I can go back to their window at anytime easily.

From the code below I'm able to get the application: pywinauto.application.Application object at 0x0000020C78574FA0

But if I try to add to a list from the command applications.extend(app) I get the error:

Exception: Object is not iterable, try to use .windows()

My script:

import pyautogui
import time
import pyperclip
from pywinauto.application import Application
from win32gui import GetWindowRect, GetForegroundWindow

website = 'https://www.google.com/'

pages = ['Profile 1']

applications = []

try:
    for page in pages:
        app = Application(backend="uia").start(r"C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe" + str(' --profile-directory="' + str(page) + '"'))
        time.sleep(3)
        GetWindowRect(GetForegroundWindow())
        time.sleep(3)
        pyautogui.hotkey('ctrl','l')

        for char in str(website):
            pyperclip.copy(char)
            pyautogui.hotkey('ctrl', 'v')
        pyautogui.hotkey('enter')
        pyautogui.hotkey('ctrl', 'f5')

        print(app)
        applications.extend(app)

except Exception as e:
    print("Exception: " + str(e))

Is there a way to add an application to a list and then iterate (open window) through them later?


Solution

  • Instead of applications.extend(app) Add the current window to list like this :
    applications.append(GetForegroundWindow())

    And later use it like this (I mean activating the browser windows) :
    SetForegroundWindow(applications[0])

    Both the Functions are the implementation win32gui.