等待直到在窗口中检测到按钮,然后使用PyWinAuto点击它。

huangapple go评论51阅读模式
英文:

Wait until Button Detected in Window Then Click It Using PyWinAuto

问题

我正在尝试自动化软件安装。在安装过程中,PyWinAuto脚本会点击“下一步”开始安装。它在安装过程中显示一个进度条动画,然后自动跳转到下一页,其中有一个点击“完成”的按钮。

我正在努力找出如何让PyWinAuto等待直到检测到“完成”按钮,然后点击它。我已经查阅了官方文档和其他Stack Overflow的问题,但没有找到一种方法来做到这一点。明确一点,安装窗口的标题在整个过程中保持不变,所以我不能只是检查新窗口标题是否存在。

我意识到有一些解决办法,比如每次等待5分钟来安装,但我希望能够有最大可靠性。

帮助将不胜感激。谢谢!

我尝试过的方法:

  • 尝试给点击按钮添加超时参数,像这样。不幸的是,这样做会导致错误。
    app.TheAppName.Done.Click(timeout=600)
  • 研究如何使用wait或wait_not(见这里)。然而,它们似乎与我的情况不相关。
  • 在尝试点击完成按钮之前等待10分钟,并且每隔60秒尝试一次,最多尝试10次。然而,这并不理想,因为如果我在慢速PC上运行脚本,可能需要更长时间,从而导致整个脚本失败。
英文:

I'm trying to automate a software installation. During the installation the PyWinAuto script clicks "Next" to start the installation. It shows a progress bar animation as it installs. Then moves to the next page automatically with a button to click "Done".

I'm trying to figure out how to have PyWinAuto wait until it detects the "Done" button then clicks it. I've reviewed the official documentation and other Stack Overflow questions but have not seen a way to do this. To be clear- the installation windows title is the same throughout the whole process so I cannot just have it check for a new window title's existence.

I realize there are workarounds such as just having it wait 5 minutes every time for it to install but I want to have the maximum amount of reliability.

app = Application(backend='uia')
app.start(programPath).connect(title=programName, timeout=120)

app.TheAppName.Install.click()
app.TheAppName.Next.click() # Starts installation

# TEMPORARY WORKAROUND: Try to click the Done button. 
# If it fails then perform a cooldown and try again until maxAttempts is reached.
maxAttempts = 10
cooldown = 60
for i in range(maxAttempts):
    try:
        app.TheAppName.Done.Click()
    except:
        print(Exception)
        time.sleep(cooldown)

Help would be greatly appreciated. Thank you!

Things I tried:

  • seeing if I could add a timeout to detect the button like so. Unfortunately this gave an error.
    app.TheAppName.Done.Click(timeout=600)
  • To figure out how I could use wait or wait_not (seen here). They did not seem relevant to my situation though.
  • Having the program wait 10 minutes before trying to click the done button, as well as, having it try every 60 seconds for a max number of 10 attempts. However, this is not ideal because if I run the script on a slow PC it may take longer and cause my whole script to fail.

答案1

得分: 1

"wait"和"wait_not"方法为什么与您的情况不相关?也许可以设置更精确的搜索条件:

app.TheAppName.child_window(title="Done", control_type="Button").wait("enabled", timeout=600).click()

有时按钮可能有热键,那么精确的标题可能看起来像"&Done"(请确保精确的文本)。这种窗口规范在搜索过程中执行精确比较。

app.TheAppName.Done这样的魔法属性查找执行近似搜索,对少数拼写错误具有抵抗力,但存在误检测其他具有类似名称的元素的风险。而且这种魔法查找通常速度较慢。对于您当前的情况,这不是关键问题。

英文:

Why wait and wait_not methods are not relevant to your situation? Maybe more precise search criteria could be set:

app.TheAppName.child_window(title="Done", control_type="Button").wait("enabled", timeout=600).click()

Also sometimes there is a hotkey for the button, then the precise title may look like "&Done" (please double check the exact text). Such kind of window specification performs exact comparison during search procedure.

Magic attribute lookup like app.TheAppName.Done performs approx. search resistant to few typos, but it has a risk or false detection of another element with similar name. And such magic lookup is typically slow. Using control_type and exact title makes search much faster. Though for your current case it's not critical.

huangapple
  • 本文由 发表于 2023年5月8日 01:44:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定