Josh S
Josh S

Reputation: 9

Pause a Python Script to wait for a PopUp Window from Microsoft Edge Web Browser

Every morning I kick off a download from a Web Based application. When the download is complete, I receive a pop up window titled "Internet Explorer" which then allows me to select between saving or opening the file or cancelling the operation. These downloads can take anywhere from 15 minutes to over an hour to complete so I would like to have a Python script that would run in the back ground and wait for the window to appear.

I do not have any code because I don't even know where to start. Is there a simple way to tell Python to wait for a specific window to open and focus on that window?

Go easy on me. Complete noob here! Any input is appreciated!

Upvotes: -3

Views: 87

Answers (1)

Sean Massey
Sean Massey

Reputation: 83

You could try the following code as a starting point. Please note that you will need to make sure you have installed PyAutoGUI, instructions can be found here 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())

Upvotes: 0

Related Questions