每天早上我都会从基于网络的应用程序开始下载。下载完成后,我会收到一个标题为“Internet Explorer”的弹出窗口,然后我可以在保存或打开文件或取消操作之间进行选择。这些下载可能需要 15 分钟到一个多小时才能完成,所以我想要一个可以在后台运行并等待窗口出现的 Python 脚本。
我没有任何代码,因为我什至不知道从哪里开始。有没有一种简单的方法来告诉 Python 等待特定窗口打开并关注该窗口?
对我宽容一些。这里完全是菜鸟!任何意见都表示赞赏!
您可以尝试以下代码作为起点。请注意,您需要确保已安装 PyAutoGUI,可以在此处找到说明 https://pypi.org/project/PyAutoGUI/
import pyautogui
import time
# Specify the file path of the file to wait for
file_path = r"C:\path\to\file.zip"
# Wait until the file has downloaded
while not pyautogui.locateOnScreen(file_path):
time.sleep(1)
# Create a pop-up window
pyautogui.alert(
text="Internet Explorer has finished downloading a file.",
title="Internet Explorer",
buttons=["Open", "Save"],
)
# Get the user's choice
choice = pyautogui.button()
# If the user chose to open the file, open it
if choice == "Open":
pyautogui.doubleClick(file_path)
# If the user chose to save the file, prompt them to choose a save location
elif choice == "Save":
pyautogui.prompt("Enter a save location for the file:")
save_location = pyautogui.prompt()
# Save the file to the specified location
with open(save_location, "wb") as f:
with open(file_path, "rb") as f2:
f.write(f2.read())