How to control a "Save as" window that opens on a webpage, using Python?

Viewed 2499

So i'm using Selenium with Python to navigate through a website, and to click a button that opens a "Save as" window. How can i control that window ? I need to send a path first where the file should be saved and also to modify the name of the file, then to click "Save".

I think i cannot do this using Selenium because the "Save as" window that opens it is a system dialog window.

What tool or Python script should i use instead to accomplish what i need to do ? How to control a system dialog window using Python ?

1 Answers

You can use a GUI automation package like pyautogui. Explanation in code comments.

import pyautogui
import time

# To simulate a Save As dialog. You can remove this since you'll be saving/downloading a file from a link
pyautogui.hotkey('ctrl', 's')
# Wait for the Save As dialog to load. Might need to increase the wait time on slower machines
time.sleep(1)
# File path + name
FILE_NAME = 'C:\\path\\to\\file\\file.ext'
# Type the file path and name is Save AS dialog
pyautogui.typewrite(FILE_NAME)
#Hit Enter to save
pyautogui.hotkey('enter')
Related