pywinautocontrolpanel

Print control identifiers for Network and Sharing Center


I am looking for a way how to change the IP address of a network adapter by a script. I am trying pywinauto.

I managed to open the Network and Sharing Center from Windows Control panels. Now I am looking for a way to click on the Change adapter settings link to get the list of my network adapters:

Screenshot highlighting the window element I am trying to click on

So I tried to print the control identifiers of this window by .print_control_identifiers()

import pywinauto

network_cpl = pywinauto.Application(backend="uia").start('control /name Microsoft.NetworkAndSharingCenter')
dlg = network_cpl["Network and Sharing Center"]
dlg.print_control_identifiers()

I have checked in the live debugging console that dlg is actually the dialogue of network_cpl:

network_cpl
<pywinauto.application.Application object at 0x000000000476FDD8>
actions:<pywinauto.actionlogger._StandardLogger object at 0x0000000003BCE630>
backend:<pywinauto.backend.BackEnd object at 0x000000000539B208>
match_history:[]
process:7888
use_history:False
xmlpath:''


dlg
<pywinauto.application.WindowSpecification object at 0x0000000003C0C828>
WAIT_CRITERIA_MAP:{'active': ('is_active',), 'enabled': ('is_enabled',), 'exists': ('exists',), 'ready': ('is_visible', 'is_enabled'), 'visible': ('is_visible',)}
actions:<pywinauto.actionlogger._StandardLogger object at 0x0000000003BCE828>
backend:<pywinauto.backend.BackEnd object at 0x000000000539B208>
criteria:[{'backend': 'uia', 'best_match': 'Network and Sharing Center', 'process': 7888}]

I see that the process ID of the dlg WindowsSpecification object is the same as the process ID of the network_cpl Application object. Yet when I execute dlg.print_control_identifiers() I get this:

Exception has occurred: pywinauto.findwindows.ElementNotFoundError
{'best_match': 'Network and Sharing Center', 'backend': 'uia', 'process': 7888}

Solution

  • This is typical problem when launcher process spawns a child one. Automatic detection of spawning processes is planned in the future.Currently you may use

    network_cpl.connect(title="Network and Sharing Center")
    

    after starting an app. Or access it through Desktop object:

    >>> from pywinauto import Desktop, Application
    
    >>> network_cpl = Application(backend="uia").start('control /name Microsoft.NetworkAndSharingCenter')
    >>> network_cpl.process
    9652
    >>> dlg_desktop = Desktop(backend="uia")["Network and Sharing Center"]
    >>> found_dlg = dlg_desktop.wrapper_object()
    >>> found_dlg.process_id()
    15520