Search code examples
autoit

ControlID changes each time a window is launched


Consider this AutoIt code:

WinActivate("Test Window")

Sleep(5000)

ControlClick("Test Window", "", 197128)

There is a button in the window I am testing. I have written a simple script to click on the button using ControlClick. I use controlID to identify and click on the button (I use AutoIt window Info to get the controlID). The script works. The problem is that the control ID changes each time the test window is launched. As a result, the script does not simulate a button click each time a new instance of the window is launched. How can I make controlclick work as expected every time?


Solution

  • For controls that have dynamic control Ids, the best is to use advanced detection.

    As it can be seen in the Help File:

    A special description can be used as the controlID parameter used in most of the Control...() functions. This description can be used to identify a control by the following properties:

    For example,

    ControlSend("Untitled - Notepad", "", "[CLASS:Edit; INSTANCE:1]", "This is some text")
    

    Or

    ControlClick("My Window", "", "[CLASS:Button; TEXT:Finish; INSTANCE:2]")
    

    Don't forget to set:

    Opt("WinTitleMatchMode", 4) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase