[Python] Python Windows UI Automation to click "close other tabs" button in Microsoft Edge...

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with the Python UIAutomation for Windows library to create keyboard shortcuts that are missing from Edge. I'd like to be able to trigger a Python script that clicks the "close other tabs" button on the browser tab right-click context menu. The following code works right up until the last step. It doesn't throw an exception, but instead clicks at a somewhat random location that doesn't correspond to the menu item I've specified. Somehow the offset seems incorrect. Any tips?

    Code below simplified to help focus the issue (e.g. no sanity checking to see if there is a successful match for each control, no exception catching).

    import uiautomation as auto

    # Find the Edge window
    edge_window = auto.WindowControl(searchDepth=1, ClassName='Chrome_WidgetWin_1', Name_re='.* - Microsoft.*Edge')

    # Find the browser tabs in the window
    tab_control = edge_window.Control(searchDepth=6, ControlType=auto.ControlType.TabControl)

    # Find the active tab from all available tabs and right-click it
    selection_pattern = tab_control.GetPattern(auto.PatternId.SelectionPattern)
    selected_tabs = selection_pattern.GetSelection()
    active_tab = select_tabs[0]
    active_tab.RightClick()

    # find the "close other tabs" from the context menu and left-click it
    closeButton = edge_window.MenuItemControl(name='Close other tabs', ControlType=auto.ControlType.MenuItemControl)
    closeButton.Click()

    Continue reading...

Compartilhe esta Página