“control.Exists” within a loop works for first time and not for second time in coded ui

前端 未结 3 472
名媛妹妹
名媛妹妹 2020-11-29 13:52

consider the code

for(i = 0; i < 5; i++)
{
    if(control.Exists)
    {
        mouse.Click(button);
    }
}

In this, control is a popup

3条回答
  •  广开言路
    2020-11-29 14:47

    Programs often draw another copy of a control, it looks identical to the eye but it is different. Hence the second time around the loop control refers to the old version of the control but it is no longer present.

    Your code is likely to be equivalent to

    for(i = 0; i < 5; i++)
    {
        if(top.middle.control.Exists)
        {
            mouse.Click(top.middle.button);
        }
    }
    

    There may be more levels in the UI Control hierarchy but three is enough for the explaination here.

    The normal fix is to find the new copy of the control before using it. So change the code to be

    for(i = 0; i < 5; i++)
    {
        top.middle.Find();
        if(top.middle.control.Exists)
        {
            mouse.Click(top.middle.button);
        }
    }
    

    If that does not work, because middle is also not available, then use top.Find();.

    To learn more about which controls are available or not, try code like this and observe which parts of the screen are highlighted with blue boxes.

    for(i = 0; i < 5; i++)
    {
        top.DrawHighLight();
        top.middle.DrawHighLight();
        top.middle.control.DrawHighLight();
        if(top.middle.control.Exists)
        {
            mouse.Click(top.middle.button);
        }
    }
    

提交回复
热议问题