如何使用Python控制在网页上打开的"Save as"window?
How to control a "Save as" window that opens on a webpage, using Python?
所以我使用带有 Python 的 Selenium 浏览网站,并单击打开 "Save as" window 的按钮。我如何控制 window ?我需要先发送文件的保存路径,还要修改文件名,然后点击"Save"。
我认为我无法使用 Selenium 执行此操作,因为打开它的 "Save as" window 是一个系统对话框 window。
我应该使用什么工具或 Python 脚本来完成我需要做的事情?
如何使用 Python 控制系统对话框 window ?
您可以使用像 pyautogui 这样的 GUI 自动化包。代码注释中的解释。
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')
所以我使用带有 Python 的 Selenium 浏览网站,并单击打开 "Save as" window 的按钮。我如何控制 window ?我需要先发送文件的保存路径,还要修改文件名,然后点击"Save"。
我认为我无法使用 Selenium 执行此操作,因为打开它的 "Save as" window 是一个系统对话框 window。
我应该使用什么工具或 Python 脚本来完成我需要做的事情? 如何使用 Python 控制系统对话框 window ?
您可以使用像 pyautogui 这样的 GUI 自动化包。代码注释中的解释。
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')