Home > Mobile >  Pywinauto: How to set focus on a window from a dataframe
Pywinauto: How to set focus on a window from a dataframe

Time:12-24

I'm trying to automate some windows tasks and I got a dataframe of all windows opens, and then I added some more coluns in order to make some validations before proceed with the automation.

Then I want to loop through all data in the column WebBrowser from the dataframe in order to set focus and activate this windows to the front, and then resize.

But I'm getting an error when I try the following command app.set_focus(): AttributeError: 'list' object has no attribute 'set_focus'.

Note: I don't know how to resize yet, I stopped in a step before, but if someone could give me a hint I'd appreciate it.

My code:

from pywinauto import Desktop
import pandas as pd

windows = Desktop(backend="uia").windows()
window = [w.window_text() for w in windows]

# Create a dataframe in order to store the windows needed
df_windows = pd.DataFrame(window, columns =['WebBrowser'])
# Filter dataframe only to show all windows from Brave web browser
df_windows = df_windows.loc[df_windows['WebBrowser'].str.contains("Brave:", case=False)]
# Add column profile from Brave
df_windows['Profile'] = df_windows['WebBrowser'].str.split(':').str[1].str.strip()
# Add column window open from Brave
df_windows['Window'] = df_windows['WebBrowser'].str.split(':').str[0].str.strip()
# Add column about the website open from Brave
df_windows['Website'] = df_windows['Window'].str.replace(" - Brave", "").str.strip()
# Filter dataframe only to show all bombcrypto game window
df_windows = df_windows.loc[df_windows['Website'] == 'GuilhermeMatheus']

print(df_windows)

for x in df_windows['WebBrowser']:
    print(x)
    app = Desktop(backend="uia").windows(title=x)
    app.set_focus()
    # resize window after

CodePudding user response:

After I run your code, I found your app in app = Desktop(backend="uia").windows(title=x) is a list. So you need to get the app element in list, that is, app[0]. Here is the fixed code and it will run correctly.

for x in df_windows['WebBrowser']:
    #get first element in list
    app = Desktop(backend="uia").windows(title=x)[0]
    app.set_focus()

And if you want resize windows, you can read article here

  • Related