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

爷,独闯天下 提交于 2019-11-26 10:03:50

问题


consider the code

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

In this, control is a popup window. for the first time of execution, control.Exists = true and for the second time it is false though the control is present. Why is that so? How to make it to be true?

Thanks in advance.


回答1:


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);
    }
}



回答2:


Use TryFind() instead, and set the searchTimeout.




回答3:


You can use Inspect/UISpy to check the RuntimeId of the control. I think it's kind of controls' version described in AdrianHHH's reply.

But, what confuses me most is that I can use the found control in the first time, in the following loops, Although my application is restarted.



来源:https://stackoverflow.com/questions/27526163/control-exists-within-a-loop-works-for-first-time-and-not-for-second-time-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!