Search code examples
pywinauto

How to connect with control.exe to get the dump_tree() in pywinauto


I want to automate below scenario through Pywinauto.

  1. Open Network and sharing Center via Control panel
  2. In the left panel of Network and sharing Center, open Change Advanced sharing setting
  3. Under network discovery, Click on ‘Turn off network discovery” option.
  4. Click “Save Changes”
  5. Go to Tools->Folder Options->View.
  6. Check 'Hide protected operating system files
  7. Close to save.

I can able to open control.exe through below code:

Application().start(r'control.exe', wait_for_idle=False)
app = Application(backend="uia").connect(path='')

I need to get dump_tree to find value on left Panel. What i need to give as path for connect() function


Solution

  • There are two ways to connect to the new window. The first one is using executable name:

    app = Application(backend="uia").connect(path='explorer.exe')
    

    But flexible waiting with timeout is not implemented for this case yet. Second way is more reliable and it doesn't require hard-coded time.sleeps.

    app = Application(backend="uia").connect(title="All Control Panel Items", timeout=10)
    

    You need explicit timeout value because of small issue that we will fix in next major release.

    To get all top level windows you need app.windows() and do .window_text() for each. This case is pretty simple:

    main_window = app.window(title="All Control Panel Items")
    # only WindowSpecification has .dump_tree() method for now
    main_window.dump_tree()