#比如自动启动QQ,匹配到密码输入位置,然后点击,输入密码。但是QQ页面没有加载好
import os
import pyautogui
path="D:\Tencent\QQ\Bin\QQScLauncher.exe"
os.startfile(path)
location=pyautogui.locateOnScreen(image='pswd.png')
print(location)
x,y=pyautogui.center(location)
print('center()',x,y)
pyautogui.click(x=x,y=y,clicks=1,button='left')
pyautogui.typewrite("pswd")
#怎样让它循环,直到位置锁定成功?
通过获取窗口句柄的方式获取qq状态,下面示例程序,程序约在窗口最前面,拿到的句柄越靠前,能拿到句柄,切QQ在最前面,说明已经加载好了,你就可以开始下面操作了,如果没有,则继续循环拿下一次句柄,也可以等待个3,500ms再次获取
获取句柄方式如下
import win32gui
hwnd_title = {}
def get_all_hwnd(hwnd,mouse):
if (win32gui.IsWindow(hwnd)
and win32gui.IsWindowEnabled(hwnd)
and win32gui.IsWindowVisible(hwnd)):
hwnd_title.update({hwnd:win32gui.GetWindowText(hwnd)})
win32gui.EnumWindows(get_all_hwnd,0)
for h,t in hwnd_title.items():
if t:
print(h,t)
有帮助请点一下右上角的采纳,谢谢
程序没有加载好,locateOnScreen应该会吧报错的,你加个死循环和try,当不报错就终止循环,否则通过循环去等待
如果不用延时,可用while循环,结合try/except异常捕获,当没有捕获到图像窗口时,一直循环下去,直到获取。参考下面代码 :
os.startfile(path)
while True:
try:
location=pyautogui.locateOnScreen(image='pswd.png',confidence=0.7)
x,y=pyautogui.center(location)
print('center()',x,y)
pyautogui.click(x=858,y=596,clicks=1,button='left')
pyautogui.typewrite("pswd")
pyautogui.click(x=950,y=668,clicks=1,button='left')
break
except:
continue
import os
import pyautogui
path="D:\BTBTBTBT\Tencent\QQ\Bin\QQScLauncher.exe"
os.startfile(path)
while True:
try:
location = pyautogui.locateOnScreen(image='pswd.png')
x, y = pyautogui.center(location)
break
except:
continue
print('center()', x, y)
pyautogui.click(x=x, y=x, clicks=1, button='left')
pyautogui.typewrite("pswd")
这样更快