Home > Software engineering >  Can someone explain this block of python code for me line by line
Can someone explain this block of python code for me line by line

Time:09-17

    import pywinauto
    desktop = pywinauto.Desktop(backend="uia")
    window = desktop.windows(title_re=".* Google Chrome$",control_type="Pane")[0]

    wrapper_list = window.descendants(control_type="TabItem")

    for wrapper in wrapper_list:
         print(wrapper.window_text())

I got this code from Get urls of opened tabs in browser pywinauto python

I'd like to know how each line of code works, If anyone could kindly explain this to me it would be much appreciated.Specially on the window.descendants(control_type="TabItem")

CodePudding user response:

wrapper_list = window.descendants(control_type="TabItem")

This will return only the list of open tab names..

["uiawrapper.UIAWrapper - 'Can someone .... line - Stack Overflow - Brave', Pane",uiawrapper.UIAWrapper - 'Downloads', TabItem]

It also returns pane also (opened browser)

Suppose if you dont provide any parameter like:

wrapper_list = window.descendants()

Every thing will come which ever present in browser but Excepts contents in browser.

Including bookmarks list ( but some times you will get browser conrtents also but sometimes not)

which looks like stackoverflow website.

uiawrapper.UIAWrapper - 'Why not use the same distribution for the prior in 
Bayesian statistics?', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Was Cain let off too lightly for murder, according to 
the biblical account?', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'What does "Danger!" mean?', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Proof of a neat pattern in polynomials', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'macOS, the command line developer tool missing after 
updated to Monterey 12.6', Hyperlink
uiawrapper.UIAWrapper - ' Question feed', Hyperlink
uiawrapper.UIAWrapper - '', GroupBox
uiawrapper.UIAWrapper - 'Stack Overflow', Hyperlink
uiawrapper.UIAWrapper - '', GroupBox
uiawrapper.UIAWrapper - 'STACK OVERFLOW', Hyperlink
uia_controls.ListViewWrapper - '', ListBox
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Questions', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Help', Hyperlink
uiawrapper.UIAWrapper - 'PRODUCTS', Hyperlink
uia_controls.ListViewWrapper - '', ListBox
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Teams', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Advertising', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Collectives', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Talent', Hyperlink
uiawrapper.UIAWrapper - 'COMPANY', Hyperlink
uia_controls.ListViewWrapper - '', ListBox
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'About', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Press', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Work Here', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Legal', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Privacy Policy', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Terms of Service', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Contact Us', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Cookie Settings', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Cookie Policy', Hyperlink
uiawrapper.UIAWrapper - 'STACK EXCHANGE NETWORK', Hyperlink
uia_controls.ListViewWrapper - '', ListBox
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Technology', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Culture & recreation', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Life & arts', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Science', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Professional', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Business', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'API', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Data', Hyperlink
uia_controls.ListViewWrapper - '', ListBox
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Blog', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Facebook', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Twitter', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'LinkedIn', Hyperlink
uia_controls.ListItemWrapper - '', ListItem
uiawrapper.UIAWrapper - 'Instagram', Hyperlink
uia_controls.StaticWrapper - 'Site design / logo © 2022 Stack Exchange Inc; user 
contributions licensed under ', Static
uiawrapper.UIAWrapper - 'CC BY-SA', Hyperlink
uia_controls.StaticWrapper - '. ', Static
uia_controls.StaticWrapper - 'rev 2022.9.16.42980', Static
######################
for wrapper in wrapper_list:
    print(wrapper) #

So if you use wrapper_list = window.descendants(control_type="MenuItem") you will get MenuItem ... Here uia_controls.MenuItemWrapper - 'Search tabs', MenuItem

MenuItem is control_Type

Finally it is used to filter out the contents.

If you nees current working pane just use "Pane".

CodePudding user response:

Yes, that can be done. See inline notes I made below.

import pywinauto # imports the pywinauto lib

 # collects the active windows on the users desktop.
desktop = pywinauto.Desktop(backend="uia") 

# search currently open windows for the regex string and return the first one
# the regex string targets open google chrome browser windows
window = desktop.windows(title_re=".* Google Chrome$",control_type="Pane")[0]


# return a list of open tabs in the browser
wrapper_list = window.descendants(control_type="TabItem")


for wrapper in wrapper_list:  # iterate through the tab list 
    print(wrapper.window_text())   # and print the tab title for each tab
  • Related