提问人:Josh S 提问时间:9/25/2023 更新时间:10/5/2023 访问量:23
暂停 Python 脚本以等待 Microsoft Edge Web 浏览器的弹出窗口
Pause a Python Script to wait for a PopUp Window from Microsoft Edge Web Browser
问:
每天早上,我都会从基于 Web 的应用程序开始下载。下载完成后,我会收到一个名为“Internet Explorer”的弹出窗口,然后允许我在保存或打开文件或取消操作之间进行选择。这些下载可能需要 15 分钟到一个多小时才能完成,所以我想要一个 Python 脚本,该脚本将在后台运行并等待窗口出现。
我没有任何代码,因为我甚至不知道从哪里开始。有没有一种简单的方法可以告诉 Python 等待特定窗口打开并专注于该窗口?
对我放轻松。这里是完整的菜鸟!任何意见都值得赞赏!
答:
0赞
Sean Massey
10/5/2023
#1
您可以尝试以下代码作为起点。请注意,您需要确保已安装 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())
评论