Get urls of opened tabs in browser pywinauto python

2k views Asked by At

Am trying to get all tabs in web browser Brave / Chrome window But don;t understand how to get the list. I am using python 3.8.13. Tried with tabs = dlg.Pane9.iter_descendants() but it results an object like <generator object BaseWrapper.iter_descendants at 0x0000024AC8FD0580> and there is no list inside.

dlg.print_control_identifiers() looks like this

['TESTING - Brave - TESTING', 'TESTING - Brave - TESTINGPane', 'Pane', 'Pane0', 'Pane1']
child_window(title="TESTING - Brave - TESTING", control_type="Pane")
   | 
   | Document - 'Powered by WordPress  Username or Email Address admin  Password       Log In  Lost your password? ← Go to Sosinventory  '    (L961, T-972, R1919, B-31)
   | ['Powered by WordPress  Username or Email Address admin  Password       Log In  Lost your password? ← Go to Sosinventory  Document', 'Document', 'Powered by WordPress  Username or Email Address admin  Password       Log In  Lost your password? ← Go to Sosinventory  ']
   | child_window(title="Powered by WordPress  Username or Email Address admin  Password       Log In  Lost your password? ← Go to Sosinventory  ", auto_id="40111424", control_type="Document")
   |    | 
   |    | Hyperlink - 'Powered by WordPress'    (L1398, T-925, R1482, B-840)
   |    | ['Hyperlink', 'Powered by WordPressHyperlink', 'Powered by WordPress', 'Hyperlink0', 'Hyperlink1']
   |    | child_window(title="Powered by WordPress", control_type="Hyperlink")
   |    | 
   |    | Static - 'Username or Email Address'    (L1305, T-788, R1475, B-768)
   |    | ['Static', 'Username or Email Address', 'Username or Email AddressStatic', 'Static0', 'Static1']
   |    | child_window(title="Username or Email Address", control_type="Text")
   |    | 
   |    | Edit - 'Username or Email Address'    (L1305, T-765, R1575, B-724)
   |    | ['Edit', 'Username or Email AddressEdit', 'Edit0', 'Edit1']
   |    | child_window(title="Username or Email Address", auto_id="user_login", control_type="Edit")
   |    | 
   |    | Static - 'Password'    (L1305, T-708, R1363, B-688)
   |    | ['Static2', 'Password', 'PasswordStatic']
   |    | child_window(title="Password", control_type="Text")
   |    | 
   |    | Edit - 'Password'    (L1305, T-685, R1575, B-644)
   |    | ['PasswordEdit', 'Edit2']
   |    | child_window(title="Password", auto_id="user_pass", control_type="Edit")
   |    | 
   |    | Button - 'Show password'    (L1535, T-685, R1575, B-644)
   |    | ['Show password', 'Show passwordButton', 'Button', 'Button0', 'Button1']
   |    | child_window(title="Show password", control_type="Button")
   |    | 
   |    | CheckBox - 'Remember Me'    (L1305, T-626, R1321, B-609)
   |    | ['Remember Me', 'CheckBox', 'Remember MeCheckBox']
   |    | child_window(title="Remember Me", auto_id="rememberme", control_type="CheckBox")
   |    | 
   |    | Button - 'Log In'    (L1513, T-629, R1575, B-596)
   |    | ['Log InButton', 'Log In', 'Button2']
   |    | child_window(title="Log In", auto_id="wp-submit", control_type="Button")
   |    | 
   |    | Hyperlink - 'Lost your password?'    (L1304, T-537, R1421, B-519)
   |    | ['Hyperlink2', 'Lost your password?', 'Lost your password?Hyperlink']
   |    | child_window(title="Lost your password?", control_type="Hyperlink")
   |    | 
   |    | Hyperlink - '← Go to Sosinventory'    (L1304, T-501, R1429, B-483)
   |    | ['Hyperlink3', '← Go to SosinventoryHyperlink', '← Go to Sosinventory']
   |    | child_window(title="← Go to Sosinventory", control_type="Hyperlink")
   | 
   | Pane - ''    (L961, T-1080, R1919, B-31)
   | ['Pane2', 'Username or Email AddressPane', 'Username or Email AddressPane0', 'Username or Email AddressPane1']
   | 
   | TitleBar - ''    (L0, T0, R0, B0)
   | ['TitleBar']
   |    | 
   |    | Menu - 'System'    (L961, T-1072, R983, B-1050)
   |    | ['Menu', 'SystemMenu', 'System', 'System0', 'System1']
   |    | child_window(title="System", auto_id="MenuBar", control_type="MenuBar")
   |    |    | 
   |    |    | MenuItem - 'System'    (L961, T-1072, R983, B-1050)
   |    |    | ['SystemMenuItem', 'MenuItem', 'System2', 'MenuItem0', 'MenuItem1']
   |    |    | child_window(title="System", control_type="MenuItem")
   |    | 
   |    | Button - 'Minimize'    (L0, T0, R0, B0)
   |    | ['Minimize', 'MinimizeButton', 'Button3', 'Minimize0', 'Minimize1', 'MinimizeButton0', 'MinimizeButton1']
   |    | child_window(title="Minimize", control_type="Button")
   |    | 
   |    | Button - 'Maximize'    (L0, T0, R0, B0)
   |    | ['Maximize', 'MaximizeButton', 'Button4', 'Maximize0', 'Maximize1', 'MaximizeButton0', 'MaximizeButton1']
   |    | child_window(title="Maximize", control_type="Button")
   |    | 
   |    | Button - 'Close'    (L0, T0, R0, B0)
   |    | ['Button5', 'Close', 'CloseButton', 'Close0', 'Close1', 'CloseButton0', 'CloseButton1']
   |    | child_window(title="Close", control_type="Button")
   | 
   | Pane - 'Brave'    (L961, T-1080, R1919, B-31)
   | ['Brave', 'BravePane', 'Pane3', 'Brave0', 'Brave1']
   | child_window(title="Brave", control_type="Pane")
   |    | 
   |    | Pane - ''    (L961, T-1080, R1919, B-31)
   |    | ['Pane4', 'Username or Email AddressPane2']
   |    |    | 
   |    |    | Pane - ''    (L1736, T-1079, R1919, B-1050)
   |    |    | ['Pane5']
   |    |    |    | 
   |    |    |    | MenuItem - 'Search tabs'    (L1736, T-1079, R1781, B-1050)
   |    |    |    | ['Search tabsMenuItem', 'Search tabs', 'MenuItem2']
   |    |    |    | child_window(title="Search tabs", control_type="MenuItem")
   |    |    |    | 
   |    |    |    | Button - 'Minimize'    (L1781, T-1079, R1827, B-1050)
   |    |    |    | ['Minimize2', 'MinimizeButton2', 'Button6']
   |    |    |    | child_window(title="Minimize", control_type="Button")
   |    |    |    | 
   |    |    |    | Button - 'Maximize'    (L1827, T-1079, R1873, B-1050)
   |    |    |    | ['Maximize2', 'MaximizeButton2', 'Button7']
   |    |    |    | child_window(title="Maximize", control_type="Button")
   |    |    |    | 
   |    |    |    | Button - 'Close'    (L1873, T-1079, R1919, B-1050)
   |    |    |    | ['Button8', 'Close2', 'CloseButton2']
   |    |    |    | child_window(title="Close", control_type="Button")
   |    |    | 
   |    |    | Pane - ''    (L961, T-1071, R1919, B-31)
   |    |    | ['Pane6', 'Username or Email AddressPane3']
   |    |    |    | 
   |    |    |    | Pane - ''    (L961, T-1071, R1919, B-972)
   |    |    |    | ['Pane7']
   |    |    |    |    | 
   |    |    |    |    | TabControl - ''    (L961, T-1071, R1736, B-1040)
   |    |    |    |    | ['TabControlNew Tab', 'TabControl']
   |    |    |    |    |    | 
   |    |    |    |    |    | Pane - ''    (L961, T-1071, R1448, B-1040)
   |    |    |    |    |    | ['Pane8']
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | Pane - ''    (L961, T-1071, R1448, B-1040)
   |    |    |    |    |    |    | ['Pane9']
   |    |    |    |    |    |    |    | 
   |    |    |    |    |    |    |    | TabItem - 'TESTING'    (L961, T-1071, R1209, B-1040)
   |    |    |    |    |    |    |    | ['TESTING', 'TabItem', 'TESTINGTabItem', 'TESTING0', 'TESTING1', 'TabItem0', 'TabItem1', 'TESTINGTabItem0', 'TESTINGTabItem1']
   |    |    |    |    |    |    |    | child_window(title="TESTING", control_type="TabItem")
   |    |    |    |    |    |    |    | 
   |    |    |    |    |    |    |    | TabItem - 'TESTING'    (L1200, T-1071, R1448, B-1040)
   |    |    |    |    |    |    |    | ['TESTING2', 'TabItem2', 'TESTINGTabItem2']
   |    |    |    |    |    |    |    | child_window(title="TESTING", control_type="TabItem")
   |    |    |    |    |    |    |    |    | 
   |    |    |    |    |    |    |    |    | Button - 'Close'    (L1420, T-1071, R1448, B-1040)
   |    |    |    |    |    |    |    |    | ['Button9', 'Close3', 'CloseButton3']
   |    |    |    |    |    |    |    |    | child_window(title="Close", control_type="Button")
   |    |    |    |    |    | 
   |    |    |    |    |    | Button - 'New Tab'    (L1448, T-1071, R1482, B-1040)
   |    |    |    |    |    | ['New Tab', 'New TabButton', 'Button10']
   |    |    |    |    |    | child_window(title="New Tab", control_type="Button")
   |    |    |    |    |    | 
   |    |    |    |    |    | Pane - ''    (L1482, T-1071, R1736, B-1040)
   |    |    |    |    |    | ['Pane10']
   |    |    |    |    | 
   |    |    |    |    | Toolbar - ''    (L961, T-1041, R1919, B-1005)
   |    |    |    |    | ['ToolbarEdit bookmark for this tab', 'ToolbarNot secure', 'ToolbarTESTING', 'ToolbarWallet', 'Toolbar', 'ToolbarForward', 'ToolbarBrave', 'ToolbarReload', 'ToolbarAddress and search bar', 'Toolbar0', 'Toolbar1']
   |    |    |    |    |    | 
   |    |    |    |    |    | Button - 'Back'    (L969, T-1037, R997, B-1009)
   |    |    |    |    |    | ['Back', 'BackButton', 'Button11']
   |    |    |    |    |    | child_window(title="Back", control_type="Button")
   |    |    |    |    |    | 
   |    |    |    |    |    | Button - 'Forward'    (L1001, T-1037, R1029, B-1009)
   |    |    |    |    |    | ['Forward', 'ForwardButton', 'Button12']
   |    |    |    |    |    | child_window(title="Forward", control_type="Button")
   |    |    |    |    |    | 
   |    |    |    |    |    | Button - 'Reload'    (L1033, T-1037, R1061, B-1009)
   |    |    |    |    |    | ['ReloadButton', 'Reload', 'Button13']
   |    |    |    |    |    | child_window(title="Reload", control_type="Button")
   |    |    |    |    |    | 
   |    |    |    |    |    | Button - 'Edit bookmark for this tab'    (L1123, T-1037, R1151, B-1009)
   |    |    |    |    |    | ['Edit bookmark for this tabButton', 'Edit bookmark for this tab', 'Button14']
   |    |    |    |    |    | child_window(title="Edit bookmark for this tab", control_type="Button")
   |    |    |    |    |    | 
   |    |    |    |    |    | MenuItem - 'Not secure'    (L1161, T-1035, R1278, B-1011)
   |    |    |    |    |    | ['Not secureMenuItem', 'Not secure', 'MenuItem3']
   |    |    |    |    |    | child_window(title="Not secure", control_type="MenuItem")
   |    |    |    |    |    | 
   |    |    |    |    |    | Edit - 'Address and search bar'    (L1278, T-1035, R1502, B-1011)
   |    |    |    |    |    | ['Edit3']
   |    |    |    |    |    | child_window(title="Address and search bar", control_type="Edit")
   |    |    |    |    |    | 
   |    |    |    |    |    | Pane - ''    (L0, T0, R0, B0)
   |    |    |    |    |    | ['Pane11']
   |    |    |    |    |    | 
   |    |    |    |    |    | Pane - ''    (L1504, T-1037, R1579, B-1009)
   |    |    |    |    |    | ['Pane12']
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | Separator - 'Separator'    (L1504, T-1031, R1511, B-1015)
   |    |    |    |    |    |    | ['SeparatorSeparator', 'Separator']
   |    |    |    |    |    |    | child_window(title="Separator", control_type="Separator")
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | MenuItem - 'Brave Shields'    (L1511, T-1035, R1545, B-1011)
   |    |    |    |    |    |    | ['Brave ShieldsMenuItem', 'MenuItem4', 'Brave Shields']
   |    |    |    |    |    |    | child_window(title="Brave Shields", control_type="MenuItem")
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | Button - 'Brave Rewards'    (L1545, T-1035, R1579, B-1011)
   |    |    |    |    |    |    | ['Brave Rewards', 'Brave RewardsButton', 'Button15']
   |    |    |    |    |    |    | child_window(title="Brave Rewards", control_type="Button")
   |    |    |    |    |    | 
   |    |    |    |    |    | Pane - ''    (L1623, T-1037, R1811, B-1009)
   |    |    |    |    |    | ['Pane13']
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | MenuItem - 'Bitdefender Anti-tracker\nHas access to this site'    (L1623, T-1037, R1651, B-1009)
   |    |    |    |    |    |    | ['Bitdefender Anti-tracker\nHas access to this siteMenuItem', 'Bitdefender Anti-tracker\nHas access to this site', 'MenuItem5']
   |    |    |    |    |    |    | child_window(title="Bitdefender Anti-tracker\nHas access to this site", control_type="MenuItem")
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | MenuItem - 'Bitdefender Wallet is on\nHas access to this site'    (L1655, T-1037, R1683, B-1009)
   |    |    |    |    |    |    | ['Bitdefender Wallet is on\nHas access to this siteMenuItem', 'MenuItem6', 'Bitdefender Wallet is on\nHas access to this site']
   |    |    |    |    |    |    | child_window(title="Bitdefender Wallet is on\nHas access to this site", control_type="MenuItem")
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | MenuItem - 'Microsoft Power Automate\nHas access to this site'    (L1687, T-1037, R1715, B-1009)
   |    |    |    |    |    |    | ['Microsoft Power Automate\nHas access to this site', 'Microsoft Power Automate\nHas access to this siteMenuItem', 'MenuItem7']
   |    |    |    |    |    |    | child_window(title="Microsoft Power Automate\nHas access to this site", control_type="MenuItem")
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | MenuItem - 'Session Buddy'    (L1719, T-1037, R1747, B-1009)
   |    |    |    |    |    |    | ['Session BuddyMenuItem', 'Session Buddy', 'MenuItem8']
   |    |    |    |    |    |    | child_window(title="Session Buddy", control_type="MenuItem")
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | MenuItem - 'Selenium IDE\nHas access to this site'    (L1751, T-1037, R1779, B-1009)
   |    |    |    |    |    |    | ['Selenium IDE\nHas access to this site', 'Selenium IDE\nHas access to this siteMenuItem', 'MenuItem9']
   |    |    |    |    |    |    | child_window(title="Selenium IDE\nHas access to this site", control_type="MenuItem")
   |    |    |    |    |    |    | 
   |    |    |    |    |    |    | MenuItem - 'Extensions'    (L1783, T-1037, R1811, B-1009)
   |    |    |    |    |    |    | ['MenuItem10', 'ExtensionsMenuItem', 'Extensions']
   |    |    |    |    |    |    | child_window(title="Extensions", control_type="MenuItem")
   |    |    |    |    |    | 
   |    |    |    |    |    | MenuItem - 'Wallet'    (L1815, T-1039, R1847, B-1007)
   |    |    |    |    |    | ['Wallet', 'WalletMenuItem', 'MenuItem11']
   |    |    |    |    |    | child_window(title="Wallet", control_type="MenuItem")
   |    |    |    |    |    | 
   |    |    |    |    |    | Button - 'TESTING'    (L1851, T-1037, R1879, B-1009)
   |    |    |    |    |    | ['TESTING3', 'Button16', 'TESTINGButton']
   |    |    |    |    |    | child_window(title="TESTING", control_type="Button")
   |    |    |    |    |    | 
   |    |    |    |    |    | MenuItem - 'Brave'    (L1883, T-1037, R1911, B-1009)
   |    |    |    |    |    | ['Brave2', 'BraveMenuItem', 'MenuItem12']
   |    |    |    |    |    | child_window(title="Brave", control_type="MenuItem")
   |    |    |    |    | 
   |    |    |    |    | Pane - ''    (L961, T-973, R1919, B-972)
   |    |    |    |    | ['Pane14']
   |    |    |    |    | 
   |    |    |    |    | Toolbar - 'Bookmarks'    (L961, T-1005, R1919, B-973)
   |    |    |    |    | ['Toolbar2', 'Bookmarks', 'BookmarksToolbar']
   |    |    |    |    | child_window(title="Bookmarks", control_type="ToolBar")
   |    |    |    |    |    | 
   |    |    |    |    |    | Button - 'Download Website'    (L969, T-1005, R1105, B-977)
   |    |    |    |    |    | ['Download Website', 'Download WebsiteButton', 'Button17']
   |    |    |    |    |    | child_window(title="Download Website", control_type="Button")
   |    |    |    |    |    | 
   |    |    |    |    |    | Button - 'INVENTORY'    (L1109, T-1005, R1209, B-977)
   |    |    |    |    |    | ['INVENTORY', 'INVENTORYButton', 'Button18']
   |    |    |    |    |    | child_window(title="INVENTORY", control_type="Button")
   |    |    |    | 
   |    |    |    | Pane - ''    (L961, T-972, R1919, B-31)
   |    |    |    | ['Pane15', 'Username or Email AddressPane4']
   |    |    |    |    | 
   |    |    |    |    | Pane - ''    (L0, T0, R0, B0)
   |    |    |    |    | ['Pane16']
   |    |    |    |    | 
   |    |    |    |    | Pane - ''    (L961, T-972, R1919, B-31)
   |    |    |    |    | ['Pane17', 'Username or Email AddressPane5']
   |    |    |    | 
   |    |    |    | Pane - ''    (L0, T0, R0, B0)
   |    |    |    | ['Pane18']
   |    |    |    | 
   |    |    |    | Pane - ''    (L0, T0, R0, B0)
   |    |    |    | ['Pane19']

CODE

from pywinauto import application as appl
from pywinauto import findwindows as fw
def startApp(app_path, backend="win32"):
    #print(app_handle)
    app = appl.Application(backend=backend)
    try:
       app.connect(path=app_path)
    except (WindowNotFoundError, ProcessNotFoundError):
       app.start(app_path)
    return app
app = startApp(bravePath, backend="uia")
time.sleep(1)
winElem = fw.find_elements(title_re=".*TESTING.*", top_level_only=False)
if winElem:
    handle = winElem[0].handle
    procId = winElem[0].process_id
    app = appl.Application(backend='uia')
    app = app.connect(path=bravePath, process=procId)
    dlg = app.window(title_re="TESTING")
    tabs = dlg.Pane9.iter_descendants() # <generator object BaseWrapper.iter_descendants at 0x0000024AC8FD0580>
1

There are 1 answers

2
David Pratmarty On BEST ANSWER

You can try this code with Chrome:

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())

It prints the name of all tabs then you just have to get the url of each tab:

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:
    wrapper.click_input()
    wrapper_url = window.descendants(title="Address and search bar", control_type="Edit")[0]
    print(wrapper_url.get_value())